|
4 | 4 | package main |
5 | 5 |
|
6 | 6 | import ( |
7 | | - "bufio" |
8 | | - "flag" |
9 | | - "fmt" |
| 7 | + "log" |
10 | 8 | "os" |
11 | 9 |
|
12 | | - "github.com/aws/amazon-cloudwatch-agent/tool/data" |
13 | | - "github.com/aws/amazon-cloudwatch-agent/tool/processors" |
14 | | - "github.com/aws/amazon-cloudwatch-agent/tool/processors/basicInfo" |
15 | | - "github.com/aws/amazon-cloudwatch-agent/tool/processors/migration/linux" |
16 | | - "github.com/aws/amazon-cloudwatch-agent/tool/processors/migration/windows" |
17 | | - "github.com/aws/amazon-cloudwatch-agent/tool/processors/serialization" |
18 | | - "github.com/aws/amazon-cloudwatch-agent/tool/processors/tracesconfig" |
19 | | - "github.com/aws/amazon-cloudwatch-agent/tool/runtime" |
20 | | - "github.com/aws/amazon-cloudwatch-agent/tool/stdin" |
21 | | - "github.com/aws/amazon-cloudwatch-agent/tool/testutil" |
22 | | - "github.com/aws/amazon-cloudwatch-agent/tool/util" |
| 10 | + "github.com/aws/amazon-cloudwatch-agent/tool/cmdwrapper" |
| 11 | + "github.com/aws/amazon-cloudwatch-agent/tool/wizard/flags" |
23 | 12 | ) |
24 | 13 |
|
25 | | -type IMainProcessor interface { |
26 | | - VerifyProcessor(processor interface{}) |
27 | | -} |
28 | | -type MainProcessorStruct struct{} |
29 | | - |
30 | | -var MainProcessorGlobal IMainProcessor = &MainProcessorStruct{} |
31 | | - |
32 | | -var isNonInteractiveWindowsMigration *bool |
33 | | - |
34 | | -var configOutputPath *string |
35 | | - |
36 | | -var isNonInteractiveXrayMigration *bool |
37 | | - |
38 | 14 | func main() { |
39 | | - // Parse command line args for non-interactive Windows migration |
40 | | - isNonInteractiveWindowsMigration = flag.Bool("isNonInteractiveWindowsMigration", false, |
41 | | - "If true, it will use command line args to bypass the wizard. Default value is false.") |
42 | | - |
43 | | - isNonInteractiveLinuxMigration := flag.Bool("isNonInteractiveLinuxMigration", false, |
44 | | - "If true, it will do the linux config migration. Default value is false.") |
45 | | - |
46 | | - tracesOnly := flag.Bool("tracesOnly", false, "If true, only trace configuration will be generated") |
47 | | - useParameterStore := flag.Bool("useParameterStore", false, |
48 | | - "If true, it will use the parameter store for the migrated config storage.") |
49 | | - isNonInteractiveXrayMigration = flag.Bool("nonInteractiveXrayMigration", false, "If true, then this is part of non Interactive xray migration tool.") |
50 | | - configFilePath := flag.String("configFilePath", "", |
51 | | - fmt.Sprintf("The path of the old config file. Default is %s on Windows or %s on Linux", windows.DefaultFilePathWindowsConfiguration, linux.DefaultFilePathLinuxConfiguration)) |
52 | | - |
53 | | - configOutputPath = flag.String("configOutputPath", "", "Specifies where to write the configuration file generated by the wizard") |
54 | | - parameterStoreName := flag.String("parameterStoreName", "", "The parameter store name. Default is AmazonCloudWatch-windows") |
55 | | - parameterStoreRegion := flag.String("parameterStoreRegion", "", "The parameter store region. Default is us-east-1") |
56 | | - |
57 | | - flag.Parse() |
| 15 | + log.Printf("Starting config-wizard, this will map back to a call to amazon-cloudwatch-agent") |
58 | 16 |
|
59 | | - if *isNonInteractiveWindowsMigration { |
60 | | - addWindowsMigrationInputs(*configFilePath, *parameterStoreName, *parameterStoreRegion, *useParameterStore) |
61 | | - } else if *isNonInteractiveLinuxMigration { |
62 | | - ctx := new(runtime.Context) |
63 | | - config := new(data.Config) |
64 | | - ctx.HasExistingLinuxConfig = true |
65 | | - ctx.ConfigFilePath = *configFilePath |
66 | | - if ctx.ConfigFilePath == "" { |
67 | | - ctx.ConfigFilePath = linux.DefaultFilePathLinuxConfiguration |
68 | | - } |
69 | | - process(ctx, config, linux.Processor, serialization.Processor) |
70 | | - return |
71 | | - } else if *tracesOnly { |
72 | | - ctx := new(runtime.Context) |
73 | | - config := new(data.Config) |
74 | | - ctx.TracesOnly = true |
75 | | - ctx.ConfigOutputPath = *configOutputPath |
76 | | - if *isNonInteractiveXrayMigration { |
77 | | - ctx.NonInteractiveXrayMigration = true |
78 | | - } |
79 | | - process(ctx, config, tracesconfig.Processor, serialization.Processor) |
80 | | - return |
81 | | - } |
82 | | - |
83 | | - startProcessing() |
84 | | -} |
85 | | - |
86 | | -func init() { |
87 | | - stdin.Scanln = func(a ...interface{}) (n int, err error) { |
88 | | - scanner := bufio.NewScanner(os.Stdin) |
89 | | - scanner.Scan() |
90 | | - if len(a) > 0 { |
91 | | - *a[0].(*string) = scanner.Text() |
92 | | - n = len(*a[0].(*string)) |
93 | | - } |
94 | | - err = scanner.Err() |
95 | | - return |
96 | | - } |
97 | | - processors.StartProcessor = basicInfo.Processor |
98 | | -} |
99 | | - |
100 | | -func addWindowsMigrationInputs(configFilePath string, parameterStoreName string, parameterStoreRegion string, useParameterStore bool) { |
101 | | - inputChan := testutil.SetUpTestInputStream() |
102 | | - if useParameterStore { |
103 | | - testutil.Type(inputChan, "2", "1", "2", "1", configFilePath, "1", parameterStoreName, parameterStoreRegion, "1") |
104 | | - } else { |
105 | | - testutil.Type(inputChan, "2", "1", "2", "1", configFilePath, "2") |
106 | | - } |
107 | | -} |
108 | | - |
109 | | -func process(ctx *runtime.Context, config *data.Config, processors ...processors.Processor) { |
110 | | - for _, processor := range processors { |
111 | | - processor.Process(ctx, config) |
112 | | - } |
113 | | -} |
114 | | - |
115 | | -func startProcessing() { |
116 | | - ctx := new(runtime.Context) |
117 | | - config := new(data.Config) |
118 | | - ctx.ConfigOutputPath = *configOutputPath |
119 | | - var processor interface{} |
120 | | - processor = processors.StartProcessor |
121 | | - if *isNonInteractiveWindowsMigration { |
122 | | - ctx.WindowsNonInteractiveMigration = true |
123 | | - } |
124 | | - if *isNonInteractiveXrayMigration { |
125 | | - ctx.NonInteractiveXrayMigration = true |
126 | | - } |
127 | | - for { |
128 | | - if processor == nil { |
129 | | - if util.CurOS() == util.OsTypeWindows && !*isNonInteractiveWindowsMigration { |
130 | | - util.EnterToExit() |
131 | | - } |
132 | | - fmt.Println("Program exits now.") |
133 | | - break |
134 | | - } |
135 | | - MainProcessorGlobal.VerifyProcessor(processor) // For testing purposes |
136 | | - processor.(processors.Processor).Process(ctx, config) |
137 | | - processor = processor.(processors.Processor).NextProcessor(ctx, config) |
138 | | - } |
139 | | -} |
| 17 | + fs, wizardFlags := cmdwrapper.CreateFlagSet(flags.Command, flags.WizardFlags) |
| 18 | + fs.Parse(os.Args[1:]) // Skip program name only |
140 | 19 |
|
141 | | -func (p *MainProcessorStruct) VerifyProcessor(processor interface{}) { |
| 20 | + _ = cmdwrapper.ExecuteSubcommand(flags.Command, wizardFlags) |
142 | 21 | } |
0 commit comments