Skip to content

Commit 90e77e4

Browse files
committed
Isolate rootCMD prerun/post run func
Do a better job of isolating code and simplify the GetRootCMD()
1 parent 7cdb0b5 commit 90e77e4

File tree

1 file changed

+42
-32
lines changed

1 file changed

+42
-32
lines changed

cmd/root/root.go

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package root
33
import (
44
"context"
55
"fmt"
6-
"github.com/replicate/pget/cmd/version"
76
"os"
87
"runtime"
98
"time"
@@ -13,6 +12,7 @@ import (
1312
"github.com/spf13/cobra"
1413
"github.com/spf13/viper"
1514

15+
"github.com/replicate/pget/cmd/version"
1616
pget "github.com/replicate/pget/pkg"
1717
"github.com/replicate/pget/pkg/cli"
1818
"github.com/replicate/pget/pkg/client"
@@ -45,37 +45,15 @@ var pidFile *cli.PIDFile
4545

4646
func GetCommand() *cobra.Command {
4747
cmd := &cobra.Command{
48-
Use: "pget [flags] <url> <dest>",
49-
Short: "pget",
50-
Long: rootLongDesc,
51-
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
52-
if err := config.PersistentStartupProcessFlags(); err != nil {
53-
return err
54-
}
55-
if cmd.CalledAs() != version.VersionCMDName {
56-
pidFilePath := viper.GetString(config.OptPIDFile)
57-
pid, err := cli.NewPIDFile(pidFilePath)
58-
if err != nil {
59-
return err
60-
}
61-
err = pid.Acquire()
62-
if err != nil {
63-
return err
64-
}
65-
pidFile = pid
66-
}
67-
return nil
68-
},
69-
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
70-
if pidFile != nil {
71-
return pidFile.Release()
72-
}
73-
return nil
74-
},
75-
PreRun: rootCmdPreRun,
76-
RunE: runRootCMD,
77-
Args: cobra.ExactArgs(2),
78-
Example: ` pget https://example.com/file.tar ./target-dir`,
48+
Use: "pget [flags] <url> <dest>",
49+
Short: "pget",
50+
Long: rootLongDesc,
51+
PersistentPreRunE: rootPersistentPreRunEFunc,
52+
PersistentPostRunE: rootPersistentPostRunEFunc,
53+
PreRun: rootCmdPreRun,
54+
RunE: runRootCMD,
55+
Args: cobra.ExactArgs(2),
56+
Example: ` pget https://example.com/file.tar ./target-dir`,
7957
}
8058
cmd.Flags().BoolP(config.OptExtract, "x", false, "OptExtract archive after download")
8159
cmd.SetUsageTemplate(cli.UsageTemplate)
@@ -109,6 +87,38 @@ func defaultPidFilePath() string {
10987
return path
11088
}
11189

90+
func pidFlock(pidFilePath string) error {
91+
pid, err := cli.NewPIDFile(pidFilePath)
92+
if err != nil {
93+
return err
94+
}
95+
err = pid.Acquire()
96+
if err != nil {
97+
return err
98+
}
99+
pidFile = pid
100+
return nil
101+
}
102+
103+
func rootPersistentPreRunEFunc(cmd *cobra.Command, args []string) error {
104+
if err := config.PersistentStartupProcessFlags(); err != nil {
105+
return err
106+
}
107+
if cmd.CalledAs() != version.VersionCMDName {
108+
if err := pidFlock(viper.GetString(config.OptPIDFile)); err != nil {
109+
return err
110+
}
111+
}
112+
return nil
113+
}
114+
115+
func rootPersistentPostRunEFunc(cmd *cobra.Command, args []string) error {
116+
if pidFile != nil {
117+
return pidFile.Release()
118+
}
119+
return nil
120+
}
121+
112122
func persistentFlags(cmd *cobra.Command) error {
113123
// Persistent Flags (applies to all commands/subcommands)
114124
cmd.PersistentFlags().IntVarP(&concurrency, config.OptConcurrency, "c", runtime.GOMAXPROCS(0)*4, "Maximum number of concurrent downloads/maximum number of chunks for a given file")

0 commit comments

Comments
 (0)