Skip to content

Commit

Permalink
Relocate cluster commands
Browse files Browse the repository at this point in the history
  • Loading branch information
sagikazarmark committed Jan 20, 2019
1 parent 8ff92d7 commit e2bff9f
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 22 deletions.
4 changes: 0 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,6 @@ func InitPipeline() *client.APIClient {
return client.NewAPIClient(config)
}

func Out1(data interface{}, fields []string) {
Out([]interface{}{data}, fields)
}

func Out(data interface{}, fields []string) {
switch rootOptions.Output {
case "json":
Expand Down
39 changes: 39 additions & 0 deletions internal/cli/command/cluster/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright © 2019 Banzai Cloud
//
// 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 cluster

import (
"github.com/banzaicloud/banzai-cli/internal/cli"
"github.com/spf13/cobra"
)

// NewClusterCommand returns a cobra command for `cluster` subcommands.
func NewClusterCommand(banzaiCli cli.Cli) *cobra.Command {
cmd := &cobra.Command{
Use: "cluster",
Aliases: []string{"clusters", "c"},
Short: "Manage clusters",
}

cmd.AddCommand(
clusterListCmd,
clusterGetCmd,
clusterCreateCmd,
clusterShellCmd,
clusterDeleteCmd,
)

return cmd
}
21 changes: 3 additions & 18 deletions cmd/cluster.go → internal/cli/command/cluster/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd
package cluster

import (
"bytes"
Expand All @@ -27,14 +27,14 @@ import (

"github.com/antihax/optional"
"github.com/banzaicloud/pipeline/client"
yaml "github.com/ghodss/yaml"
"github.com/ghodss/yaml"
"github.com/goph/emperror"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ttacon/chalk"
survey "gopkg.in/AlecAivazis/survey.v1"
"gopkg.in/AlecAivazis/survey.v1"
)

const clusterIdKey = "cluster.id"
Expand All @@ -43,14 +43,6 @@ var clusterOptions struct {
Name string
}

// clusterCmd represents the cluster command
var clusterCmd = &cobra.Command{
Use: "cluster",
Aliases: []string{"clusters", "c"},
Short: "Handle clusters",
Run: ClusterList,
}

var clusterListCmd = &cobra.Command{
Use: "list",
Aliases: []string{"l"},
Expand Down Expand Up @@ -426,13 +418,6 @@ func GetClusterId(org int32, ask bool) int32 {
}

func init() {
rootCmd.AddCommand(clusterCmd)
clusterCmd.AddCommand(clusterListCmd)
clusterCmd.AddCommand(clusterGetCmd)
clusterCmd.AddCommand(clusterCreateCmd)
clusterCmd.AddCommand(clusterShellCmd)
clusterCmd.AddCommand(clusterDeleteCmd)

clusterShellCmd.PersistentFlags().Int32("cluster", 0, "cluster id")
viper.BindPFlag(clusterIdKey, clusterShellCmd.PersistentFlags().Lookup("cluster"))
clusterShellCmd.PersistentFlags().StringVar(&clusterOptions.Name, "cluster-name", "", "cluster name")
Expand Down
67 changes: 67 additions & 0 deletions internal/cli/command/cluster/legacy_organization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright © 2019 Banzai Cloud
//
// 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 cluster

import (
"context"

log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"gopkg.in/AlecAivazis/survey.v1"
)

const orgIdKey = "organization.id"

func searchOrganizationId(name string) int32 {
pipeline := InitPipeline()
orgs, _, err := pipeline.OrganizationsApi.ListOrgs(context.Background())
if err != nil {
logAPIError("list organizations", err, nil)
return 0
}
for _, org := range orgs {
if org.Name == name {
return org.Id
}
}
log.Errorf("Could not find organization %q", name)
return 0
}

func GetOrgId(ask bool) int32 {
id := viper.GetInt32(orgIdKey)
if id != 0 {
return id
}
if ask && !isInteractive() {
log.Fatal("No organization is selected. Use the --organization switch, or set it using `banzai org`.")
}
pipeline := InitPipeline()
orgs, _, err := pipeline.OrganizationsApi.ListOrgs(context.Background())
if err != nil {
log.Fatalf("could not list organizations: %v", err)
}
orgSlice := make([]string, len(orgs))
for i, org := range orgs {
orgSlice[i] = org.Name
}
name := ""
survey.AskOne(&survey.Select{Message: "Organization:", Options: orgSlice}, &name, survey.Required)
id = searchOrganizationId(name)
if id != 0 {
viper.Set(orgIdKey, id)
}
return id
}
94 changes: 94 additions & 0 deletions internal/cli/command/cluster/legacy_root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright © 2019 Banzai Cloud
//
// 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 cluster

import (
"encoding/json"
"fmt"
"log"
"os"

"github.com/banzaicloud/banzai-cli/pkg/formatting"
"github.com/banzaicloud/pipeline/client"
"github.com/mattn/go-isatty"
"github.com/spf13/viper"
"golang.org/x/oauth2"
"gopkg.in/yaml.v2"
)

var rootOptions struct {
CfgFile string
Output string
}

func InitPipeline() *client.APIClient {
config := client.NewConfiguration()
config.BasePath = viper.GetString("pipeline.basepath")
config.UserAgent = "banzai-cli/1.0.0/go"
config.HTTPClient = oauth2.NewClient(nil, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: viper.GetString("pipeline.token")},
))

return client.NewAPIClient(config)
}

func Out1(data interface{}, fields []string) {
Out([]interface{}{data}, fields)
}

func Out(data interface{}, fields []string) {
switch rootOptions.Output {
case "json":
bytes, err := json.MarshalIndent(data, "", " ")
if err != nil {
log.Fatalf("can't marshal output: %v", err)
}
fmt.Printf("%s\n", bytes)

case "yaml":
bytes, err := yaml.Marshal(data)
if err != nil {
log.Fatalf("can't marshal output: %v", err)
}
fmt.Printf("%s\n", bytes)

default:
table := formatting.NewTable(data, fields)
out := table.Format(isColor())
fmt.Println(out)
}
}

func logAPIError(action string, err error, request interface{}) {
if err, ok := err.(client.GenericOpenAPIError); ok {
log.Printf("failed to %s: %v (err %[2]T, request=%+v, response=%s)", action, err, request, err.Body())
} else {
log.Printf("failed to %s: %v", action, err)
}
}

func isInteractive() bool {
if isatty.IsTerminal(os.Stdout.Fd()) && isatty.IsTerminal(os.Stdin.Fd()) {
return !viper.GetBool("formatting.no-interactive")
}
return viper.GetBool("formatting.force-interactive")
}

func isColor() bool {
if isatty.IsTerminal(os.Stdout.Fd()) {
return !viper.GetBool("formatting.no-color")
}
return viper.GetBool("formatting.force-color")
}
2 changes: 2 additions & 0 deletions internal/cli/command/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package command

import (
"github.com/banzaicloud/banzai-cli/internal/cli"
"github.com/banzaicloud/banzai-cli/internal/cli/command/cluster"
"github.com/banzaicloud/banzai-cli/internal/cli/command/organization"
"github.com/banzaicloud/banzai-cli/internal/cli/command/secret"
"github.com/spf13/cobra"
Expand All @@ -24,6 +25,7 @@ import (
// AddCommands adds all the commands from cli/command to the root command
func AddCommands(cmd *cobra.Command, banzaiCli cli.Cli) {
cmd.AddCommand(
cluster.NewClusterCommand(banzaiCli),
organization.NewOrganizationCommand(banzaiCli),
secret.NewSecretCommand(banzaiCli),
)
Expand Down

0 comments on commit e2bff9f

Please sign in to comment.