forked from CiscoDevNet/catalyst-center-rna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs.go
99 lines (85 loc) · 2.8 KB
/
args.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
// Copyright (c) 2023 Cisco and/or its affiliates.
// This software is licensed to you under the terms of the Cisco Sample
// Code License, Version 1.1 (the "License"). You may obtain a copy of the
// License at
// https://developer.cisco.com/docs/licenses
// All use of the material herein must be in accordance with the terms of
// the License. All rights not expressly granted by the License are
// reserved. Unless required by applicable law or agreed to separately in
// writing, software distributed under the License is distributed on an "AS
// IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied.
package main
import (
"bufio"
"fmt"
"os"
"strings"
"syscall"
"github.com/alexflint/go-arg"
"github.com/joho/godotenv"
"golang.org/x/crypto/ssh/terminal"
)
const resultZip = "dnac_rna_hc_collection.zip"
// input collects CLI input.
func input(prompt string) string {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("%s ", prompt)
input, _ := reader.ReadString('\n')
return strings.Trim(input, "\r\n")
}
// Args are command line parameters.
type Args struct {
Dnac string `arg:"-a,--address" help:"Cisco DNA Center hostname or IP address"`
Username string `arg:"-u,--username" help:"Cisco DNA Center username"`
Password string `arg:"-p,--password" help:"Cisco DNA Center password"`
Output string `arg:"-o,--output" help:"Output file"`
RequestRetryCount int `arg:"--request-retry-count" default:"3" help:"Times to retry a failed request"`
RetryDelay int `arg:"--retry-delay" default:"10" help:"Seconds to wait before retry"`
BatchSize int `arg:"--batch-size" default:"10" help:"Max request to send in parallel"`
}
// Description is the CLI description string.
func (Args) Description() string {
return "RNA - Rapid Network Assesment for Cisco DNA Center."
}
// Version is the CLI version string.
func (Args) Version() string {
return fmt.Sprintf("version %s", Version)
}
func isEnvExist(key string) bool {
if _, ok := os.LookupEnv(key); ok {
return true
}
return false
}
// NewArgs collects the CLI args and creates a new 'Args'.
func newArgs() Args {
args := Args{Output: resultZip}
arg.MustParse(&args)
if _, err := os.Stat(".env"); err == nil {
godotenv.Load(".env")
if isEnvExist("DNAC_IP") {
args.Dnac = os.Getenv("DNAC_IP")
}
if isEnvExist("DNAC_USER") {
args.Username = os.Getenv("DNAC_USER")
}
if isEnvExist("PASSWORD") {
args.Password = os.Getenv("PASSWORD")
}
} else {
if args.Dnac == "" {
args.Dnac = input("DNAC IP:")
}
if args.Username == "" {
args.Username = input("Username:")
}
if args.Password == "" {
fmt.Print("Password: ")
pwd, _ := terminal.ReadPassword(int(syscall.Stdin))
args.Password = string(pwd)
fmt.Println()
}
}
return args
}