diff --git a/cmd/upreq/root.go b/cmd/upreq/root.go index 5067f19..4363f36 100644 --- a/cmd/upreq/root.go +++ b/cmd/upreq/root.go @@ -10,14 +10,24 @@ import ( var rootCmd = &cobra.Command{ Use: "upreq", Short: "upreq - a small CLI to help manage your requirements.txt file", - Long: `upreq is a small CLI to help manage your requirements.txt file.`, - Args: cobra.ExactArgs(0), + Long: `upreq - a small CLI to help manage your requirements.txt file + +For more information, visit: https://github.com/robswc/upreq + +`, + Args: cobra.ExactArgs(0), Run: func(cmd *cobra.Command, args []string) { + // get the flags + file, _ := cmd.Flags().GetString("file") + strip, _ := cmd.Flags().GetBool("strip") + git, _ := cmd.Flags().GetBool("git") + // grab current requirements - var file = cmd.Flag("file").Value.String() - var oldReqs = upreq.GetReqs(file) - fmt.Printf("Found (%[1]s) requirements in %[2]s\n", fmt.Sprint(len(oldReqs)), file) + var oldReqs = upreq.GetReqs(file, strip) + if !strip { + fmt.Printf("Found (%[1]s) requirements in %[2]s\n", fmt.Sprint(len(oldReqs)), file) + } // wipe the file upreq.WipeFile(file) @@ -30,8 +40,15 @@ var rootCmd = &cobra.Command{ upreq.DisplayDiff(diff, cmd.Flag("strip").Value.String()) // write new requirements - var writtenReqs = upreq.WriteReqs(file, newReqs) - fmt.Printf("Wrote (%[1]s) requirements to %[2]s\n", fmt.Sprint(len(writtenReqs)), file) + var writtenReqs = upreq.WriteReqs(file, newReqs, strip) + if !strip { + fmt.Printf("Wrote (%[1]s) new requirements to %[2]s\n", fmt.Sprint(len(writtenReqs)), file) + } + + // add the file to git + if git { + upreq.GitAdd(file, strip) + } }, } @@ -39,9 +56,9 @@ var rootCmd = &cobra.Command{ func Execute() { // add all the flags - rootCmd.Flags().StringP("file", "f", "requirements.txt", "Specify the requirements file to use") - rootCmd.Flags().BoolP("strip", "s", false, "Strips '+' and '-' from the output") - rootCmd.Flags().BoolP("git", "g", false, "Automatically add the file to git") + rootCmd.Flags().StringP("file", "f", "requirements.txt", "Specify the requirements file") + rootCmd.Flags().BoolP("strip", "s", false, "Strips all feedback from the output (useful for piping)") + rootCmd.Flags().BoolP("git", "g", false, "Automatically add the file to git, after writing") // run root command if err := rootCmd.Execute(); err != nil { diff --git a/pkg/upreq/upreq.go b/pkg/upreq/upreq.go index 032d8c0..d9855bb 100644 --- a/pkg/upreq/upreq.go +++ b/pkg/upreq/upreq.go @@ -9,11 +9,13 @@ import ( "strings" ) -func GetReqs(path string) []string { +func GetReqs(path string, strip bool) []string { // check that the file exists if _, err := os.Stat(path); os.IsNotExist(err) { - fmt.Println("No current requirements file found.") + if !strip { + fmt.Println("No current requirements file found.") + } return nil } @@ -61,7 +63,7 @@ func WipeFile(path string) { } } -func WriteReqs(path string, reqs []string) []string { +func WriteReqs(path string, reqs []string, strip bool) []string { // check that the file exists, if not create it if _, err := os.Stat(path); os.IsNotExist(err) { @@ -94,7 +96,7 @@ func WriteReqs(path string, reqs []string) []string { log.Fatal(err) } } - return GetReqs(path) + return GetReqs(path, strip) } func DiffCheck(oldReqs []string, newReqs []string) []string { @@ -137,3 +139,13 @@ func GetEnvReqs() []string { reqs := strings.Fields(string(out)) return reqs } + +func GitAdd(path string, strip bool) { + add := exec.Command("git", "add", path) + err := add.Run() + if err != nil { + if !strip { + fmt.Println(err) + } + } +}