-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
root.go
109 lines (97 loc) · 3.27 KB
/
root.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Copyright © 2023 David Aparicio david.aparicio@free.fr
*/
package cmd
import (
"errors"
"fmt"
"os"
"syscall"
"github.com/davidaparicio/cuc/internal"
"github.com/spf13/cobra"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
Timeout = 1
HTTPCode = 200
)
var (
logger *zap.Logger
// sugar *zap.SugaredLogger
verbose bool
url string
musicFile string
timeout int
backoff int
httpCode int
// cfgFile string
printVersion bool
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "cuc",
Short: "A simple URL checker",
Long: `A very simple CLI tool to check various HTTP status.
CUC can loop until the desired HTTP status is reached.
For example:
If a concert ticket webpage is available (200), or not found (404).`,
Run: func(cmd *cobra.Command, args []string) {
if printVersion {
internal.PrintVersion(cmd)
} else {
internal.CheckURL(url, musicFile, timeout, httpCode, false, logger, cmd)
}
},
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.ExecuteContext(rootCmd.Context())
if err != nil {
os.Exit(1)
}
}
//nolint:gochecknoinits
func init() {
rootCmd.PersistentFlags().StringVarP(&url, "url", "u", "https://www.example.com/", "Webpage to check")
rootCmd.PersistentFlags().StringVarP(&musicFile,
"musicFile", "f", "./assets/mp3/ubuntu_desktop_login.mp3", "MP3 file to play if the check is successful")
rootCmd.PersistentFlags().IntVarP(&timeout, "timeout", "t", Timeout, "Timeout in seconds")
rootCmd.PersistentFlags().IntVarP(&httpCode, "httpCode", "c", HTTPCode, "HTTP Status Code from 100 to 511")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "d", false, "Enables debug logging")
rootCmd.PersistentFlags().BoolVarP(&printVersion, "version", "v", false, "Print the version")
rootCmd.AddCommand(loopCmd)
loopCmd.PersistentFlags().IntVarP(&backoff, "backoff", "b", Backoff, "Backoff in seconds")
rootCmd.AddCommand(versionCmd)
cobra.OnInitialize(initConfig)
}
func initConfig() {
zapOptions := []zap.Option{
// skip the first caller, which is typically the logger's own function
zap.AddCallerSkip(1),
zap.AddStacktrace(zapcore.FatalLevel),
}
if !verbose { // level "debug" is not allowed by existing core
zapOptions = append(zapOptions, zap.IncreaseLevel(zapcore.FatalLevel))
}
l, err := zap.NewProduction(zapOptions...)
if err != nil {
fmt.Println("Error during setting the Uber Zap logging")
}
defer func() { // flushes buffer, if any
err := logger.Sync()
// NOTE: we use syscall.EBADF to check if the error is specifically related to a bad file descriptor,
// which should be the case for if the stderr is a TTY.
// https://github.com/uber-go/zap/issues/880#issuecomment-906074498
// https://github.com/uber-go/zap/issues/991#issuecomment-1485659915
if err != nil && (!errors.Is(err, syscall.EBADF) && !errors.Is(err, syscall.ENOTTY)) {
fmt.Println("Error during flushing all logger buffers (l.Sync()): " + err.Error())
}
}()
// L returns the global Logger, which can be reconfigured with ReplaceGlobals.
// It's safe for concurrent use.
undo := zap.ReplaceGlobals(l)
logger = zap.L()
undo()
}