diff --git a/cmd/list.go b/cmd/list.go new file mode 100644 index 0000000..86542e7 --- /dev/null +++ b/cmd/list.go @@ -0,0 +1,127 @@ +/* +Copyright © 2019 Zee Ahmed + +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 ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + "os" + "strings" + + "github.com/spf13/cobra" +) + +const ( + binaryListURL = "https://api.github.com/repos/kubernetes/kubernetes/releases?per_page=100" +) + +var ( + remote bool +) + +type kubectlVersion struct { + TagName string `json:"tag_name"` +} + +var listCmd = &cobra.Command{ + Use: "list", + Short: "List downloaded kubectl binaries", + Run: func(cmd *cobra.Command, args []string) { + var versions []kubectlVersion + if remote { + fmt.Println("Fetching remote versions ...") + versions = fetchRemoteVersions() + } else { + versions = fetchLocalVersions() + + if len(versions) > 0 { + fmt.Println("Installed kubectl versions:") + } else { + fmt.Println("No versions installed. See 'kubemngr list --remote' for available versions.") + } + } + + for _, version := range versions { + // only show stable releases + filterStable := strings.NewReplacer( + "-rc.1", "", + "-beta.2", "", + "-beta.1", "", + "-alpha.3", "", + "-alpha.2", "", + "-alpha.1", "", + "-rc.2", "", + "-rc.3", "", + ) + stable := filterStable.Replace(version.TagName) + + fmt.Println(stable) + } + }, +} + +func init() { + rootCmd.AddCommand(listCmd) + listCmd.Flags().BoolVar(&remote, "remote", false, "Get versions from remote") +} + +// fetchLocalVersions - List available installed kubectl versions +func fetchLocalVersions() []kubectlVersion { + homeDir, err := os.UserHomeDir() + if err != nil { + log.Fatal(err) + } + + kubectl, err := ioutil.ReadDir(homeDir + "/.kubemngr/") + if err != nil { + log.Fatal(err) + } + + list := []kubectlVersion{} + for _, files := range kubectl { + file := files.Name() + version := strings.Replace(file, "kubectl-", "", -1) + list = append(list, kubectlVersion{TagName: version}) + } + + return list +} + +// fetchRemoteVersions lists Kubectl binaries available at the configured remote location +func fetchRemoteVersions() []kubectlVersion { + res, err := http.Get(binaryListURL) + if err != nil { + log.Fatal(err) + } + defer res.Body.Close() + + body, err := ioutil.ReadAll(res.Body) + if err != nil { + log.Fatal(err) + } + + list := []kubectlVersion{} + jsonErr := json.Unmarshal(body, &list) + if jsonErr != nil { + log.Fatal(jsonErr) + } + + return list +} diff --git a/cmd/listRemote_darwin.go b/cmd/listRemote_darwin.go deleted file mode 100644 index 995883e..0000000 --- a/cmd/listRemote_darwin.go +++ /dev/null @@ -1,92 +0,0 @@ -// +build darwin - -/* -Copyright © 2019 Zee Ahmed - -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 ( - "encoding/json" - "fmt" - "io/ioutil" - "log" - "net/http" - "strings" - - "github.com/spf13/cobra" -) - -type Kubernetes []struct { - TagName string `json:"tag_name"` -} - -// listRemoteCmd represents the listRemote command -var listRemoteCmd = &cobra.Command{ - Use: "listRemote", - Short: "List available remote kubectl versions to download and install", - Run: func(cmd *cobra.Command, args []string) { - - fmt.Println("Getting available list of kubectl versions to install") - - k8s := Kubernetes{} - _ = ListAvailableRemotes("https://api.github.com/repos/kubernetes/kubernetes/releases?per_page=100", &k8s) - - for _, releases := range k8s { - version := releases.TagName - - // filter out alpha and release candidatest and only show stable releases - filterStable := strings.NewReplacer("-rc.1", "", "-beta.2", "", "-beta.1", "", "-alpha.3", "", "-alpha.2", "", "-alpha.1", "", "-rc.2", "", "-rc.3", "") - stable := filterStable.Replace(version) - - fmt.Println(stable) - } - }, -} - -func init() { - rootCmd.AddCommand(listRemoteCmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // listRemoteCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // listRemoteCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") -} - -func ListAvailableRemotes(url string, target interface{}) error { - - res, err := http.Get(url) - if err != nil { - log.Fatal(err) - } - - defer res.Body.Close() - - body, err := ioutil.ReadAll(res.Body) - if err != nil { - log.Fatal(err) - } - - jsonErr := json.Unmarshal(body, &target) - if jsonErr != nil { - log.Fatal(jsonErr) - } - - return jsonErr -} diff --git a/cmd/listRemote_linux.go b/cmd/listRemote_linux.go deleted file mode 100644 index a125c7c..0000000 --- a/cmd/listRemote_linux.go +++ /dev/null @@ -1,92 +0,0 @@ -// +build linux - -/* -Copyright © 2019 Zee Ahmed - -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 ( - "encoding/json" - "fmt" - "io/ioutil" - "log" - "net/http" - "strings" - - "github.com/spf13/cobra" -) - -type Kubernetes []struct { - TagName string `json:"tag_name"` -} - -// listRemoteCmd represents the listRemote command -var listRemoteCmd = &cobra.Command{ - Use: "listRemote", - Short: "List available remote kubectl versions to download and install", - Run: func(cmd *cobra.Command, args []string) { - - fmt.Println("Getting available list of kubectl versions to install") - - k8s := Kubernetes{} - _ = ListAvailableRemotes("https://api.github.com/repos/kubernetes/kubernetes/releases?per_page=100", &k8s) - - for _, releases := range k8s { - version := releases.TagName - - // filter out alpha and release candidatest and only show stable releases - filterStable := strings.NewReplacer("-rc.1", "", "-beta.2", "", "-beta.1", "", "-alpha.3", "", "-alpha.2", "", "-alpha.1", "", "-rc.2", "", "-rc.3", "") - stable := filterStable.Replace(version) - - fmt.Println(stable) - } - }, -} - -func init() { - rootCmd.AddCommand(listRemoteCmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // listRemoteCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // listRemoteCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") -} - -func ListAvailableRemotes(url string, target interface{}) error { - - res, err := http.Get(url) - if err != nil { - log.Fatal(err) - } - - defer res.Body.Close() - - body, err := ioutil.ReadAll(res.Body) - if err != nil { - log.Fatal(err) - } - - jsonErr := json.Unmarshal(body, &target) - if jsonErr != nil { - log.Fatal(jsonErr) - } - - return jsonErr -} diff --git a/cmd/list_darwin.go b/cmd/list_darwin.go deleted file mode 100644 index aaa3703..0000000 --- a/cmd/list_darwin.go +++ /dev/null @@ -1,81 +0,0 @@ -// +build darwin - -/* -Copyright © 2019 Zee Ahmed - -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 ( - "fmt" - "io/ioutil" - "log" - "os" - "strings" - - "github.com/spf13/cobra" -) - -// listCmd represents the list command -var listCmd = &cobra.Command{ - Use: "list", - Short: "List downloaded kubectl binaries", - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("Retrieving installed versions of kubectl") - - err := ListKubectlBinaries() - if err != nil { - log.Fatal(err) - } - }, -} - -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") -} - -// ListKubectlBinaries - List available installed kubectl versions -func ListKubectlBinaries() error { - - // Get user home directory path - homeDir, err := os.UserHomeDir() - if err != nil { - log.Fatal(err) - } - - // Read kubemngr directory - kubectl, err := ioutil.ReadDir(homeDir + "/.kubemngr/") - if err != nil { - log.Fatal(err) - } - - // iterate and print files inside the kubemngr directory - for _, files := range kubectl { - file := files.Name() - version := strings.Replace(file, "kubectl-", "", -1) - fmt.Println(version) - } - - return nil -} diff --git a/cmd/list_linux.go b/cmd/list_linux.go deleted file mode 100644 index 8a2db1f..0000000 --- a/cmd/list_linux.go +++ /dev/null @@ -1,81 +0,0 @@ -// +build linux - -/* -Copyright © 2019 Zee Ahmed - -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 ( - "fmt" - "io/ioutil" - "log" - "os" - "strings" - - "github.com/spf13/cobra" -) - -// listCmd represents the list command -var listCmd = &cobra.Command{ - Use: "list", - Short: "List downloaded kubectl binaries", - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("Retrieving installed versions of kubectl") - - err := ListKubectlBinaries() - if err != nil { - log.Fatal(err) - } - }, -} - -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") -} - -// ListKubectlBinaries - List available installed kubectl versions -func ListKubectlBinaries() error { - - // Get user home directory path - homeDir, err := os.UserHomeDir() - if err != nil { - log.Fatal(err) - } - - // Read kubemngr directory - kubectl, err := ioutil.ReadDir(homeDir + "/.kubemngr/") - if err != nil { - log.Fatal(err) - } - - // iterate and print files inside the kubemngr directory - for _, files := range kubectl { - file := files.Name() - version := strings.Replace(file, "kubectl-", "", -1) - fmt.Println(version) - } - - return nil -}