github.com/gernest/nezuko@v0.1.2/internal/modcmd/init.go (about)

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // z mod init
     6  
     7  package modcmd
     8  
     9  import (
    10  	"os"
    11  	"strings"
    12  
    13  	"github.com/gernest/nezuko/internal/base"
    14  	"github.com/gernest/nezuko/internal/modload"
    15  )
    16  
    17  var cmdInit = &base.Command{
    18  	UsageLine: "z mod init [module]",
    19  	Short:     "initialize new module in current directory",
    20  	Long: `
    21  Init initializes and writes a new z.mod to the current directory,
    22  in effect creating a new module rooted at the current directory.
    23  The file z.mod must not already exist.
    24  	`,
    25  	Run: runInit,
    26  }
    27  
    28  func runInit(cmd *base.Command, args []string) {
    29  	modload.CmdModInit = true
    30  	if len(args) > 1 {
    31  		base.Fatalf("z mod init: too many arguments")
    32  	}
    33  	if len(args) == 1 {
    34  		modload.CmdModModule = args[0]
    35  	}
    36  	if _, err := os.Stat("z.mod"); err == nil {
    37  		base.Fatalf("z mod init: z.mod already exists")
    38  	}
    39  	if strings.Contains(modload.CmdModModule, "@") {
    40  		base.Fatalf("z mod init: module path must not contain '@'")
    41  	}
    42  	modload.InitMod() // does all the hard work
    43  }