-
Notifications
You must be signed in to change notification settings - Fork 843
/
list.go
118 lines (95 loc) · 3.69 KB
/
list.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
110
111
112
113
114
115
116
117
118
// +build go1.9
// Copyright 2018 Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to 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.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"io"
"io/ioutil"
"log"
"os"
"path"
"github.com/Azure/azure-sdk-for-go/tools/profileBuilder/model"
"github.com/marstr/randname"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var listFlags = viper.New()
const (
inputLongName = "input"
inputShortName = "i"
inputDefault = "<stdin>"
inputDescription = "Specify a file to read for the list of packages, instead of stdin."
)
// listCmd represents the list command
var listCmd = &cobra.Command{
Use: "list",
Short: "Creates a profile from a set of packages.",
Long: `Reads a list of packages from stdin, where each line is treated as a Go package
identifier. These packages are then used to create a profile.
Often, the easiest way of invoking this command will be using a pipe operator
to specify the packages to include.
Example:
$> ../model/testdata/smallProfile.txt > profileBuilder list --name small_profile
`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
logWriter := ioutil.Discard
if viper.GetBool("verbose") {
logWriter = os.Stdout
}
outputLog := log.New(logWriter, "[STATUS] ", 0)
errLog := log.New(os.Stderr, "[ERROR] ", 0)
outputLog.Printf("Output-Location set to: %s", viper.GetString(outputLocationLongName))
var input io.Reader
if _, ok := listFlags.Get(inputLongName).(int); ok {
input = os.Stdin
} else if fileHandle, err := os.Open(listFlags.GetString(inputLongName)); err == nil {
input = fileHandle
} else {
errLog.Printf("Fatal! Unable to open file %q", listFlags.GetString(inputLongName))
return
}
deleteLoc := path.Join(listFlags.GetString(outputLocationLongName), listFlags.GetString(nameLongName))
if viper.GetBool("clear-output") {
if err := model.DeleteChildDirs(deleteLoc); err != nil {
errLog.Print("Fatal! Unable to clear output-folder:", err)
return
}
}
model.BuildProfile(
&model.ListStrategy{Reader: input},
listFlags.GetString(nameLongName),
listFlags.GetString(outputLocationLongName),
outputLog,
errLog)
},
}
func init() {
rootCmd.AddCommand(listCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// listCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// listCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
listCmd.Flags().StringP(outputLocationLongName, outputLocationShortName, outputLocationDefault, outputLocationDescription)
listCmd.Flags().StringP(nameLongName, nameShortName, nameDefault, nameDescription)
listCmd.Flags().StringP(inputLongName, inputShortName, inputDefault, inputDescription)
listFlags.BindPFlags(listCmd.Flags())
listFlags.SetDefault(nameLongName, randname.Generate())
// To work around the fact that cobra's default and viper's default are going to step on eachother's toes,
// set the viper default to an int. That way we can check based on type, instead of having a special case string.
listFlags.SetDefault(inputLongName, 0)
}