-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ff92d7
commit e2bff9f
Showing
6 changed files
with
205 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters