github.com/jingweno/gh@v2.1.1-0.20221007190738-04a7985fa9a1+incompatible/commands/init.go (about) 1 package commands 2 3 import ( 4 "github.com/jingweno/gh/github" 5 "github.com/jingweno/gh/utils" 6 ) 7 8 var cmdInit = &Command{ 9 Run: gitInit, 10 GitExtension: true, 11 Usage: "init -g", 12 Short: "Create an empty git repository or reinitialize an existing one", 13 Long: `Create a git repository as with git-init(1) and add remote origin at 14 "git@github.com:USER/REPOSITORY.git"; USER is your GitHub username and 15 REPOSITORY is the current working directory's basename. 16 `, 17 } 18 19 func init() { 20 CmdRunner.Use(cmdInit) 21 } 22 23 /* 24 $ gh init -g 25 > git init 26 > git remote add origin git@github.com:USER/REPO.git 27 */ 28 func gitInit(command *Command, args *Args) { 29 if !args.IsParamsEmpty() { 30 err := transformInitArgs(args) 31 utils.Check(err) 32 } 33 } 34 35 func transformInitArgs(args *Args) error { 36 if !parseInitFlag(args) { 37 return nil 38 } 39 40 name, err := utils.DirName() 41 if err != nil { 42 return err 43 } 44 45 project := github.NewProject("", name, "") 46 url := project.GitURL("", "", true) 47 args.After("git", "remote", "add", "origin", url) 48 49 return nil 50 } 51 52 func parseInitFlag(args *Args) bool { 53 if i := args.IndexOfParam("-g"); i != -1 { 54 args.RemoveParam(i) 55 return true 56 } 57 58 return false 59 }