-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (65 loc) · 1.87 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
// Generic Flags for most commands
flagAWSRegion = "aws-region"
)
func initViper(cmd *cobra.Command) (*viper.Viper, error) {
v := viper.New()
errBind := v.BindPFlags(cmd.Flags())
if errBind != nil {
return v, fmt.Errorf("error binding flag set to viper: %w", errBind)
}
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
v.AutomaticEnv() // set environment variables to overwrite config
return v, nil
}
// stringSliceContains returns true if a string is found inside of a string slice
func stringSliceContains(stringSlice []string, value string) bool {
for _, x := range stringSlice {
if value == x {
return true
}
}
return false
}
func main() {
rootCommand := &cobra.Command{
Use: `safelock [flags]`,
DisableFlagsInUseLine: true,
Short: "SafeLock is a golang package for locking files used by distributed services",
}
fileLockCommand := &cobra.Command{
Use: `file [flags]`,
DisableFlagsInUseLine: true,
Short: "Use SafeLock on a local file",
SilenceErrors: true,
SilenceUsage: true,
RunE: fileLockCmd,
}
initFileLockFlags(fileLockCommand.Flags())
s3ObjectLockCommand := &cobra.Command{
Use: `s3object [flags]`,
DisableFlagsInUseLine: true,
Short: "Use SafeLock on an S3 Object",
SilenceErrors: true,
SilenceUsage: true,
RunE: s3ObjectLockCmd,
}
initS3ObjectLockFlags(s3ObjectLockCommand.Flags())
rootCommand.AddCommand(
fileLockCommand,
s3ObjectLockCommand,
)
if err := rootCommand.Execute(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, "safelock: "+err.Error())
_, _ = fmt.Fprintln(os.Stderr, "Try safelock --help for more information.")
os.Exit(1)
}
}