aboutsummaryrefslogtreecommitdiff
path: root/cmd/root.go
blob: 00563c6314b70afab2bfb787eef2d69c1a40205a (plain)
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
package cmd

import (
	"context"
	"fmt"
	"github.com/bryanmcnulty/adauth"
	"github.com/rs/zerolog"
	"github.com/spf13/cobra"
	"os"
	"regexp"
)

var (
	//logFile string
	log      zerolog.Logger
	ctx      context.Context
	authOpts *adauth.Options

	debug, trace   bool
	command        string
	executablePath string
	executableArgs string

	rootCmd = &cobra.Command{
		Use: "goexec",
		PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
			// For modules that require a full executable path
			if executablePath != "" && !regexp.MustCompile(`^([a-zA-Z]:)?\\`).MatchString(executablePath) {
				return fmt.Errorf("executable path (-e) must be an absolute Windows path, i.e. C:\\Windows\\System32\\cmd.exe")
			}
			log = zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).Level(zerolog.InfoLevel).With().Timestamp().Logger()
			if debug {
				log = log.Level(zerolog.DebugLevel)
			}
			return
		},
	}
)

func init() {
	ctx = context.Background()

	rootCmd.InitDefaultVersionFlag()
	rootCmd.InitDefaultHelpCmd()
	rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "Enable debug logging")

	authOpts = &adauth.Options{Debug: log.Debug().Msgf}
	authOpts.RegisterFlags(rootCmd.PersistentFlags())

	scmrCmdInit()
	rootCmd.AddCommand(scmrCmd)
}

func Execute() {
	if err := rootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}