-
Notifications
You must be signed in to change notification settings - Fork 148
/
main.go
93 lines (77 loc) · 2.53 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/lkarlslund/adalanche/modules/integrations/localmachine"
"github.com/lkarlslund/adalanche/modules/integrations/localmachine/collect"
"github.com/lkarlslund/adalanche/modules/ui"
"github.com/lkarlslund/adalanche/modules/version"
"github.com/spf13/cobra"
)
// This is a wrapper for the new style using AIO executable, for people using the collector
// You should move to the AIO version, using "adalanche collect", but for now the standalone collect
// binary is still available
var (
wrapcollector = &cobra.Command{}
datapath = wrapcollector.Flags().String("outputpath", "", "Dump output JSON file in this folder")
loglevel = wrapcollector.Flags().String("loglevel", "info", "Console log level")
logfile = wrapcollector.Flags().String("logfile", "", "Log file")
logfilelevel = wrapcollector.Flags().String("logfilelevel", "info", "Log file log level")
)
func init() {
wrapcollector.RunE = Execute
}
func Execute(cmd *cobra.Command, args []string) error {
ll, err := ui.LogLevelString(*loglevel)
if err != nil {
ui.Error().Msgf("Invalid log level: %v - use one of: %v", *loglevel, ui.LogLevelStrings())
} else {
ui.SetLoglevel(ll)
}
if *logfile != "" {
ll, err = ui.LogLevelString(*logfilelevel)
if err != nil {
ui.Error().Msgf("Invalid log file log level: %v - use one of: %v", *logfilelevel, ui.LogLevelStrings())
} else {
ui.SetLogFile(*logfile, ll)
}
}
outputpath := *datapath
err = os.MkdirAll(outputpath, 0600)
if err != nil {
return fmt.Errorf("Problem accessing output folder: %v", err)
}
info, err := collect.Collect()
if err != nil {
return err
}
if outputpath == "" {
ui.Warn().Msg("Missing -outputpath parameter - writing file to current directory")
outputpath = "."
}
targetname := info.Machine.Name + localmachine.Suffix
if info.Machine.IsDomainJoined {
targetname = info.Machine.Name + "$" + info.Machine.Domain + localmachine.Suffix
}
output, err := json.MarshalIndent(info, "", " ")
if err != nil {
return fmt.Errorf("Problem marshalling JSON: %v", err)
}
outputfile := filepath.Join(outputpath, targetname)
err = ioutil.WriteFile(outputfile, output, 0600)
if err != nil {
return fmt.Errorf("Problem writing to file %v: %v", outputfile, err)
}
ui.Info().Msgf("Information collected to file %v", outputfile)
return nil
}
func main() {
ui.Info().Msg(version.VersionString())
err := wrapcollector.Execute()
if err != nil {
ui.Error().Err(err).Msg("Failed to execute")
}
}