forked from Azure/azure-sdk-for-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
latest.go
117 lines (93 loc) · 3.45 KB
/
latest.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
// +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/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"
)
const (
previewLongName = "preview"
previewShortName = "p"
previewDefault = false
previewDescription = "Include preview API Versions."
)
const (
rootLongName = "root"
rootShortName = "r"
rootDescription = "The location of the API Version folders which should be considered for `latest`."
)
var rootDefault = model.DefaultInputRoot()
var latestFlags = viper.New()
// latestCmd represents the latest command
var latestCmd = &cobra.Command{
Use: "latest",
Short: "Reflects on the available packages, choosing the most recent ones.",
Long: `Scans through the availabe API Versions, and chooses only the most
recent functionality.
By default, this command ignores API versions that are in preview.`,
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)
packageStrategy := model.LatestStrategy{
Root: latestFlags.GetString(rootLongName),
Predicate: model.IgnorePreview,
VerboseOutput: outputLog,
}
if latestFlags.GetBool(previewLongName) {
packageStrategy.Predicate = model.AcceptAll
outputLog.Println("Using preview versions.")
}
deleteLoc := path.Join(latestFlags.GetString(outputLocationLongName), latestFlags.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(
packageStrategy,
latestFlags.GetString(nameLongName),
latestFlags.GetString(outputLocationLongName),
outputLog,
errLog)
},
}
func init() {
rootCmd.AddCommand(latestCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// latestCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// latestCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
latestCmd.Flags().BoolP(previewLongName, previewShortName, previewDefault, previewDescription)
latestCmd.Flags().StringP(outputLocationLongName, outputLocationShortName, outputLocationDefault, outputLocationDescription)
latestCmd.Flags().StringP(nameLongName, nameShortName, nameDefault, nameDescription)
latestCmd.Flags().StringP(rootLongName, rootShortName, rootDefault, rootDescription)
latestFlags.BindPFlags(latestCmd.Flags())
latestFlags.SetDefault(nameLongName, randname.Generate())
}