github.com/grantbow/fit@v0.7.1-0.20220916164603-1f7c88ac81e6/fitapp/Create.go (about) 1 package fitapp 2 3 import ( 4 "fmt" 5 bugs "github.com/grantbow/fit/issues" 6 "io/ioutil" 7 "log" 8 "os" 9 "os/exec" 10 "strings" 11 ) 12 13 //var dops = bugs.Directory(os.PathSeparator) 14 //var sops = string(os.PathSeparator) 15 16 // filecp copies files. 17 // see https://opensource.com/article/18/6/copying-files-go 18 func filecp(sourceFile string, destinationFile string) { 19 input, err := ioutil.ReadFile(sourceFile) 20 if err != nil { 21 fmt.Println(err) 22 return 23 } 24 err = ioutil.WriteFile(destinationFile, input, 0644) 25 if err != nil { 26 fmt.Println("Error creating", destinationFile) 27 fmt.Println(err) 28 return 29 } 30 } 31 32 // Create is a subcommand to open a new issue. 33 func Create(Args argumentList, config bugs.Config) { 34 //fmt.Print("a\n") 35 if len(Args) < 1 || (len(Args) < 2 && Args[0] == "-n") { 36 //fmt.Print("b\n") 37 fmt.Fprintf(os.Stderr, "Usage: %s create [-n] <Bug Title>\n", os.Args[0]) 38 fmt.Fprintf(os.Stderr, "\nNo bug title provided.\n") 39 return 40 } 41 var noDesc bool = false 42 43 if Args.HasArgument("-n") { 44 noDesc = true 45 Args = Args[1:] 46 } 47 48 Args, argVals := Args.GetAndRemoveArguments([]string{"--tag", "--status", "--priority", "--milestone", "--identifier", "--id"}) 49 tag := argVals[0] 50 status := argVals[1] 51 priority := argVals[2] 52 milestone := argVals[3] 53 identifier := argVals[4] + argVals[5] 54 55 if Args.HasArgument("--generate-id") { 56 for i, token := range Args { 57 if token == "--generate-id" { 58 if i+1 < len(Args) { 59 Args = append(Args[:i], Args[i+1:]...) 60 break 61 } else { 62 Args = Args[:i] 63 break 64 } 65 } 66 } 67 identifier = generateID(strings.Join(Args, " ")) 68 } 69 70 // It's possible there were arguments provided, but still no title 71 // included. Do another check before trying to create the bug. 72 if strings.TrimSpace(strings.Join(Args, " ")) == "" { 73 fmt.Fprintf(os.Stderr, "Usage: %s create [-n] <Bug Title>\n", os.Args[0]) 74 fmt.Fprintf(os.Stderr, "\nNo bug title provided.\n") 75 return 76 } 77 var bgid = bugs.FitDirer(config) 78 if bgid == "" { 79 os.MkdirAll(config.FitDirName, 0700) 80 bgid = bugs.FitDirer(config) 81 } 82 var bug = bugs.Issue{ 83 Dir: bgid + dops + bugs.TitleToDir(strings.Join(Args, " ")), 84 DescriptionFileName: config.DescriptionFileName, 85 } 86 87 dir := bug.Direr() 88 89 var mode os.FileMode 90 mode = 0775 91 err := os.Mkdir(string(dir), mode) 92 if err != nil { 93 fmt.Fprintf(os.Stderr, "\n%s error: mkdir\n", os.Args[0]) 94 log.Fatal(err) 95 } 96 DescriptionFile := string(dir) + sops + config.DescriptionFileName 97 //DescriptionFile := config.DescriptionFileName 98 //if string(dir) != "" { 99 // DescriptionFile = string(dir) + sops + config.DescriptionFileName 100 //} // might fix a cygwin issue 101 if noDesc { 102 txt := []byte("") 103 if config.DefaultDescriptionFile != "" { 104 filecp(config.FitYmlDir+sops+config.DefaultDescriptionFile, DescriptionFile) 105 } else { 106 //fmt.Printf("here %s\n", config.DescriptionFileName) 107 ioutil.WriteFile(DescriptionFile, txt, 0644) 108 } 109 } else { 110 if config.DefaultDescriptionFile != "" { 111 filecp(config.FitYmlDir+sops+config.DefaultDescriptionFile, DescriptionFile) 112 } 113 cmd := exec.Command(getEditor(), DescriptionFile) 114 115 //osi := os.Stdin 116 //oso := os.Stdout 117 //ose := os.Stderr 118 cmd.Stdin = os.Stdin 119 cmd.Stdout = os.Stdout 120 cmd.Stderr = os.Stderr 121 err := cmd.Run() 122 if err != nil { 123 log.Fatal(err) 124 } 125 } 126 127 if tag != "" { 128 bug.TagIssue(bugs.TagBoolTrue(tag), config) 129 } 130 if status != "" { 131 bug.SetStatus(status, config) 132 } 133 if priority != "" { 134 bug.SetPriority(priority, config) 135 } 136 if milestone != "" { 137 bug.SetMilestone(milestone, config) 138 } 139 if identifier != "" { 140 bug.SetIdentifier(identifier, config) 141 } 142 fmt.Printf("Created issue: %s\n", bug.Title("")) 143 }