Skip to content
This repository has been archived by the owner on Mar 6, 2020. It is now read-only.

Commit

Permalink
refactored: directory structure
Browse files Browse the repository at this point in the history
  • Loading branch information
bharath-srinivas committed Feb 2, 2018
1 parent f307aa6 commit 95c32a7
Show file tree
Hide file tree
Showing 24 changed files with 474 additions and 397 deletions.
15 changes: 6 additions & 9 deletions cmd/aws-go/cmd.go → cmd/aws-go/command/cmd.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package cmd implements all the commands used by aws-go.
package cmd
package command

import (
"fmt"
Expand Down Expand Up @@ -28,20 +28,17 @@ to login to the AWS console built to be fast and easy to use.`
// AWS Session instance.
var Session *session.Session

// list of spinner prefixes.
var spinnerPrefix = []string{
"",
"\x1b[36mfetching\x1b[m ",
"\x1b[36mprocessing\x1b[m ",
}

// Main command.
var Command = &cobra.Command{
Use: "aws-go",
Long: description,
RunE: run,
}

func AddCommand(cmd *cobra.Command) {
Command.AddCommand(cmd)
}

// Execute executes the provided command.
func Execute() {
Command.Execute()
Expand All @@ -54,6 +51,6 @@ func run(cmd *cobra.Command, args []string) error {
}

// preRun will initialize the session required for all the child commands.
func preRun(cmd *cobra.Command, args []string) {
func PreRun(cmd *cobra.Command, args []string) {
Session = function.NewSession()
}
13 changes: 7 additions & 6 deletions cmd/aws-go/list.go → cmd/aws-go/ec2/list.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package ec2

import (
"fmt"
Expand All @@ -8,28 +8,29 @@ import (
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"

"github.com/bharath-srinivas/aws-go/cmd/aws-go/command"
"github.com/bharath-srinivas/aws-go/function"
"github.com/bharath-srinivas/aws-go/spinner"
"github.com/bharath-srinivas/aws-go/internal/spinner"
)

// list command.
var listCmd = &cobra.Command{
Use: "list",
Short: "List all the available EC2 instances",
Args: cobra.NoArgs,
PreRun: preRun,
PreRun: command.PreRun,
Run: listInstances,
}

func init() {
Command.AddCommand(listCmd)
command.AddCommand(listCmd)
}

// run command.
func listInstances(cmd *cobra.Command, args []string) {
sp := spinner.Default(spinnerPrefix[1])
sp := spinner.Default(spinner.Prefix[1])
sp.Start()
sess := ec2.New(Session)
sess := ec2.New(command.Session)

ec2Service := &function.EC2Service{
Service: sess,
Expand Down
13 changes: 7 additions & 6 deletions cmd/aws-go/start.go → cmd/aws-go/ec2/start.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package cmd
package ec2

import (
"fmt"

"github.com/aws/aws-sdk-go/service/ec2"
"github.com/spf13/cobra"

"github.com/bharath-srinivas/aws-go/cmd/aws-go/command"
"github.com/bharath-srinivas/aws-go/function"
"github.com/bharath-srinivas/aws-go/spinner"
"github.com/bharath-srinivas/aws-go/internal/spinner"
)

// dryRun enabled.
Expand All @@ -19,20 +20,20 @@ var startCmd = &cobra.Command{
Short: "Start the specified EC2 instance",
Args: cobra.ExactArgs(1),
Example: "aws-go start i-0a12b345c678de",
PreRun: preRun,
PreRun: command.PreRun,
Run: startInstance,
}

func init() {
Command.AddCommand(startCmd)
command.AddCommand(startCmd)
startCmd.Flags().BoolVarP(&dryRun, "dry-run", "", false, "perform the operation with dry run enabled")
}

// run command.
func startInstance(cmd *cobra.Command, args []string) {
sp := spinner.Default(spinnerPrefix[2])
sp := spinner.Default(spinner.Prefix[2])
sp.Start()
sess := ec2.New(Session)
sess := ec2.New(command.Session)

instanceId := function.EC2{
ID: args[0],
Expand Down
13 changes: 7 additions & 6 deletions cmd/aws-go/stop.go → cmd/aws-go/ec2/stop.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package cmd
package ec2

import (
"fmt"

"github.com/aws/aws-sdk-go/service/ec2"
"github.com/spf13/cobra"

"github.com/bharath-srinivas/aws-go/cmd/aws-go/command"
"github.com/bharath-srinivas/aws-go/function"
"github.com/bharath-srinivas/aws-go/spinner"
"github.com/bharath-srinivas/aws-go/internal/spinner"
)

// stop instance command.
Expand All @@ -16,20 +17,20 @@ var stopCmd = &cobra.Command{
Short: "Stop the specified EC2 instance",
Args: cobra.ExactArgs(1),
Example: "aws-go stop i-0a12b345c678de",
PreRun: preRun,
PreRun: command.PreRun,
Run: stopInstance,
}

func init() {
Command.AddCommand(stopCmd)
command.AddCommand(stopCmd)
stopCmd.Flags().BoolVarP(&dryRun, "dry-run", "", false, "perform the operation with dry run enabled")
}

// run command.
func stopInstance(cmd *cobra.Command, args []string) {
sp := spinner.Default(spinnerPrefix[2])
sp := spinner.Default(spinner.Prefix[2])
sp.Start()
sess := ec2.New(Session)
sess := ec2.New(command.Session)

instanceId := function.EC2{
ID: args[0],
Expand Down
26 changes: 26 additions & 0 deletions cmd/aws-go/env/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package env

import (
"os"

"github.com/spf13/cobra"

"github.com/bharath-srinivas/aws-go/store"
)

// env create command.
var createCmd = &cobra.Command{
Use: "create",
Short: "Create a new AWS profile with specified region (if provided)",
Example: " aws-go env create --profile staging --region us-west-1",
Run: createEnv,
}

// env create run command.
func createEnv(cmd *cobra.Command, args []string) {
if store.Profile == "" {
cmd.Usage()
os.Exit(0)
}
store.SetCredentials()
}
49 changes: 9 additions & 40 deletions cmd/aws-go/env.go → cmd/aws-go/env/env.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package cmd
// Package env manages the environments for aws-go.
package env

import (
"os"

"github.com/bharath-srinivas/aws-go/store"
"github.com/spf13/cobra"

"github.com/bharath-srinivas/aws-go/cmd/aws-go/command"
"github.com/bharath-srinivas/aws-go/store"
)

// enable env listing.
Expand All @@ -19,27 +22,11 @@ var envCmd = &cobra.Command{
Short: "Manage AWS profile configurations",
Example: ` aws-go env --list
aws-go env --delete staging`,
Run: envRun,
}

// env create command.
var createCmd = &cobra.Command{
Use: "create",
Short: "Create a new AWS profile with specified region (if provided)",
Example: " aws-go env create --profile staging --region us-west-1",
Run: createEnv,
}

// env use command.
var useCmd = &cobra.Command{
Use: "use",
Short: "Use the specified AWS profile and region (if provided)",
Example: " aws-go env use --profile staging --region eu-west-1",
Run: useEnv,
Run: run,
}

func init() {
Command.AddCommand(envCmd)
command.AddCommand(envCmd)

envCmd.AddCommand(createCmd)
envCmd.AddCommand(useCmd)
Expand All @@ -54,8 +41,8 @@ func init() {
useCmd.Flags().StringVarP(&store.Region, "region", "r", "us-east-1", "the region to use")
}

// env run command.
func envRun(cmd *cobra.Command, args []string) {
// run command.
func run(cmd *cobra.Command, args []string) {
if !listEnv && delEnv == "" {
cmd.Usage()
os.Exit(0)
Expand All @@ -67,21 +54,3 @@ func envRun(cmd *cobra.Command, args []string) {
store.DeleteProfile(delEnv)
}
}

// env create run command.
func createEnv(cmd *cobra.Command, args []string) {
if store.Profile == "" {
cmd.Usage()
os.Exit(0)
}
store.SetCredentials()
}

// env use run command.
func useEnv(cmd *cobra.Command, args []string) {
if store.Profile == "" {
cmd.Usage()
os.Exit(0)
}
store.UseProfile()
}
26 changes: 26 additions & 0 deletions cmd/aws-go/env/use.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package env

import (
"os"

"github.com/spf13/cobra"

"github.com/bharath-srinivas/aws-go/store"
)

// env use command.
var useCmd = &cobra.Command{
Use: "use",
Short: "Use the specified AWS profile and region (if provided)",
Example: " aws-go env use --profile staging --region eu-west-1",
Run: useEnv,
}

// env use run command.
func useEnv(cmd *cobra.Command, args []string) {
if store.Profile == "" {
cmd.Usage()
os.Exit(0)
}
store.UseProfile()
}
Loading

0 comments on commit 95c32a7

Please sign in to comment.