Skip to content

Commit

Permalink
add flags
Browse files Browse the repository at this point in the history
  • Loading branch information
robswc committed Oct 6, 2022
1 parent f0aa9dd commit 0dbffed
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
37 changes: 27 additions & 10 deletions cmd/upreq/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -30,18 +40,25 @@ 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)
}

},
}

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 {
Expand Down
20 changes: 16 additions & 4 deletions pkg/upreq/upreq.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit 0dbffed

Please sign in to comment.