Skip to content

Commit

Permalink
Merge pull request #45 from AckeeCZ/feature/use
Browse files Browse the repository at this point in the history
added support for use subcommand
  • Loading branch information
smoliji committed Mar 17, 2021
2 parents 38f2b0c + 6bb6ec5 commit a029207
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.5.0] - 2021-03-17
### Added
- Add `use` subcommand as convenient shorthand for setting default `gcloud` project. With support of interactive or autocompletion `-project` mode.

## [1.4.0] - 2020-12-17
### Changed
- Select proxy type automatically when `sql_instance` is passed
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- `goproxie version` to print the version
- Use default `goproxie` to start interactive wizard
- Use `goproxie history` to pick a used proxy settings
- Use `goproxie use` to interactively select and set your default GCP project. `-project` flag available.
- Use `goproxie -project=... -cluster=...` for non-interactive mode, see `--help` for all the options available

## Installation
Expand Down
7 changes: 7 additions & 0 deletions internal/gcloud/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ func ContainerClustersList(projectID string) []*Cluster {
// return Cluster{name: results[0], location: results[1]}
}

// gcloud config set project PROJECT

//SetDefaultProject sets the default Project for the gcloud cli
func SetDefaultProject(projectID string) {
util.RunSilentCommand(gcloudPath, "config", "set", "project", projectID)
}

// GetClusterCredentials gets credentials for the given GCP cluster
func GetClusterCredentials(projectID string, cluster *Cluster) {
util.RunSilentCommand(gcloudPath, "container", "clusters", "get-credentials", cluster.Name, "--project", projectID, "--zone", cluster.Location)
Expand Down
50 changes: 36 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var gcloudProjectsList = gcloud.ProjectsList
var kubectlPodsList = kubectl.PodsList
var gcloudContainerClustersList = gcloud.ContainerClustersList
var gcloudGetClusterCredentials = gcloud.GetClusterCredentials
var gcloudSetProject = gcloud.SetDefaultProject
var kubectlNamespacesList = kubectl.NamespacesList
var kubectlPortForward = kubectl.PortForward

Expand Down Expand Up @@ -316,19 +317,21 @@ func readRemotePort(containerPorts []int) (port int) {
return n
}

func readArguments() {
gcloudPath := flag.String("gcloud_path", "gcloud", "gcloud binary path")
kubectlPath := flag.String("kubectl_path", "kubectl", "kubectl binary path")
flags.project = flag.String("project", "", "Auto GCP Project pick")
flags.proxyType = flag.String("proxy_type", "", "Auto Proxy type pick")
flags.cluster = flag.String("cluster", "", "Auto Cluster pick")
flags.namespace = flag.String("namespace", "", "Auto Namespace pick")
flags.pod = flag.String("pod", "", "Auto Pod pick")
flags.localPort = flag.String("local_port", "", "Auto Local port pick")
flags.remotePort = flag.String("remote_port", "", "Auto Remote port pick")
flags.noSave = flag.Bool("no-save", false, "Don't save invocation to history")
flags.sqlInstance = flag.String("sql_instance", "", "Cloud SQL Instance in form project:region:instance-name. Can be used if you dont have permissions to list the GCP project.")
flag.Parse()
func readArguments(index int) {
flagSet := flag.NewFlagSet("", flag.ExitOnError)
gcloudPath := flagSet.String("gcloud_path", "gcloud", "gcloud binary path")
kubectlPath := flagSet.String("kubectl_path", "kubectl", "kubectl binary path")
flags.project = flagSet.String("project", "", "Auto GCP Project pick")
flags.proxyType = flagSet.String("proxy_type", "", "Auto Proxy type pick")
flags.cluster = flagSet.String("cluster", "", "Auto Cluster pick")
flags.namespace = flagSet.String("namespace", "", "Auto Namespace pick")
flags.pod = flagSet.String("pod", "", "Auto Pod pick")
flags.localPort = flagSet.String("local_port", "", "Auto Local port pick")
flags.remotePort = flagSet.String("remote_port", "", "Auto Remote port pick")
flags.noSave = flagSet.Bool("no-save", false, "Don't save invocation to history")
flags.sqlInstance = flagSet.String("sql_instance", "", "Cloud SQL Instance in form project:region:instance-name. Can be used if you dont have permissions to list the GCP project.")

flagSet.Parse(os.Args[index:])
gcloud.SetGcloudPath(*gcloudPath)
kubectl.SetKubectlPath(*kubectlPath)
}
Expand All @@ -338,22 +341,41 @@ func isBlindCloudSQLConnection() bool {
}

func main() {

if len(os.Args) < 2 {
readArguments(1)
} else {
switch os.Args[1] {
case "version", "history", "use":
readArguments(2)
default:
readArguments(1)
}
}

if len(os.Args) > 1 && os.Args[1] == "version" {
fmt.Println(version.Get())
return
}

readArguments()
store.Initialize()
if len(os.Args) > 1 && os.Args[1] == "history" {
history.Browse()
return
}

projectID := readProjectID()
if projectID == "" && !isBlindCloudSQLConnection() {
fmt.Println("Could not find any GCP Projects")
return
}

if len(os.Args) > 1 && os.Args[1] == "use" {
gcloudSetProject(projectID)
fmt.Printf("Set gcloud default project to: %s", projectID)
return
}

proxyType := readProxyType()
if proxyType == ProxyTypePod {
cluster := readCluster(projectID)
Expand Down

0 comments on commit a029207

Please sign in to comment.