Skip to content

Commit 0dbffed

Browse files
committed
add flags
1 parent f0aa9dd commit 0dbffed

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

cmd/upreq/root.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,24 @@ import (
1010
var rootCmd = &cobra.Command{
1111
Use: "upreq",
1212
Short: "upreq - a small CLI to help manage your requirements.txt file",
13-
Long: `upreq is a small CLI to help manage your requirements.txt file.`,
14-
Args: cobra.ExactArgs(0),
13+
Long: `upreq - a small CLI to help manage your requirements.txt file
14+
15+
For more information, visit: https://github.com/robswc/upreq
16+
17+
`,
18+
Args: cobra.ExactArgs(0),
1519
Run: func(cmd *cobra.Command, args []string) {
1620

21+
// get the flags
22+
file, _ := cmd.Flags().GetString("file")
23+
strip, _ := cmd.Flags().GetBool("strip")
24+
git, _ := cmd.Flags().GetBool("git")
25+
1726
// grab current requirements
18-
var file = cmd.Flag("file").Value.String()
19-
var oldReqs = upreq.GetReqs(file)
20-
fmt.Printf("Found (%[1]s) requirements in %[2]s\n", fmt.Sprint(len(oldReqs)), file)
27+
var oldReqs = upreq.GetReqs(file, strip)
28+
if !strip {
29+
fmt.Printf("Found (%[1]s) requirements in %[2]s\n", fmt.Sprint(len(oldReqs)), file)
30+
}
2131

2232
// wipe the file
2333
upreq.WipeFile(file)
@@ -30,18 +40,25 @@ var rootCmd = &cobra.Command{
3040
upreq.DisplayDiff(diff, cmd.Flag("strip").Value.String())
3141

3242
// write new requirements
33-
var writtenReqs = upreq.WriteReqs(file, newReqs)
34-
fmt.Printf("Wrote (%[1]s) requirements to %[2]s\n", fmt.Sprint(len(writtenReqs)), file)
43+
var writtenReqs = upreq.WriteReqs(file, newReqs, strip)
44+
if !strip {
45+
fmt.Printf("Wrote (%[1]s) new requirements to %[2]s\n", fmt.Sprint(len(writtenReqs)), file)
46+
}
47+
48+
// add the file to git
49+
if git {
50+
upreq.GitAdd(file, strip)
51+
}
3552

3653
},
3754
}
3855

3956
func Execute() {
4057

4158
// add all the flags
42-
rootCmd.Flags().StringP("file", "f", "requirements.txt", "Specify the requirements file to use")
43-
rootCmd.Flags().BoolP("strip", "s", false, "Strips '+' and '-' from the output")
44-
rootCmd.Flags().BoolP("git", "g", false, "Automatically add the file to git")
59+
rootCmd.Flags().StringP("file", "f", "requirements.txt", "Specify the requirements file")
60+
rootCmd.Flags().BoolP("strip", "s", false, "Strips all feedback from the output (useful for piping)")
61+
rootCmd.Flags().BoolP("git", "g", false, "Automatically add the file to git, after writing")
4562

4663
// run root command
4764
if err := rootCmd.Execute(); err != nil {

pkg/upreq/upreq.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import (
99
"strings"
1010
)
1111

12-
func GetReqs(path string) []string {
12+
func GetReqs(path string, strip bool) []string {
1313

1414
// check that the file exists
1515
if _, err := os.Stat(path); os.IsNotExist(err) {
16-
fmt.Println("No current requirements file found.")
16+
if !strip {
17+
fmt.Println("No current requirements file found.")
18+
}
1719
return nil
1820
}
1921

@@ -61,7 +63,7 @@ func WipeFile(path string) {
6163
}
6264
}
6365

64-
func WriteReqs(path string, reqs []string) []string {
66+
func WriteReqs(path string, reqs []string, strip bool) []string {
6567

6668
// check that the file exists, if not create it
6769
if _, err := os.Stat(path); os.IsNotExist(err) {
@@ -94,7 +96,7 @@ func WriteReqs(path string, reqs []string) []string {
9496
log.Fatal(err)
9597
}
9698
}
97-
return GetReqs(path)
99+
return GetReqs(path, strip)
98100
}
99101

100102
func DiffCheck(oldReqs []string, newReqs []string) []string {
@@ -137,3 +139,13 @@ func GetEnvReqs() []string {
137139
reqs := strings.Fields(string(out))
138140
return reqs
139141
}
142+
143+
func GitAdd(path string, strip bool) {
144+
add := exec.Command("git", "add", path)
145+
err := add.Run()
146+
if err != nil {
147+
if !strip {
148+
fmt.Println(err)
149+
}
150+
}
151+
}

0 commit comments

Comments
 (0)