cmd

package
v0.0.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	AddressFlag = cli.StringFlag{
		Name:  "address",
		Value: api.DefaultAddress,
		Usage: "Nullstone API Address",
	}
	ApiKeyFlag = cli.StringFlag{
		Name:  "api-key",
		Value: "",
		Usage: "Nullstone API Key",
	}
)
View Source
var Configure = cli.Command{
	Name: "configure",
	Flags: []cli.Flag{
		AddressFlag,
		ApiKeyFlag,
	},
	Action: func(c *cli.Context) error {
		apiKey := c.String(ApiKeyFlag.Name)
		if apiKey == "" {
			fmt.Print("Enter API Key: ")
			rawApiKey, err := terminal.ReadPassword(int(syscall.Stdin))
			if err != nil {
				return fmt.Errorf("error reading password: %w", err)
			}
			fmt.Println()
			apiKey = string(rawApiKey)
		}

		profile := config.Profile{
			Name:    GetProfile(c),
			Address: c.String(AddressFlag.Name),
			ApiKey:  apiKey,
		}
		if err := profile.Save(); err != nil {
			return fmt.Errorf("error configuring profile: %w", err)
		}
		fmt.Fprintln(os.Stderr, "nullstone configured successfully!")
		return nil
	},
}
View Source
var Deploy = func(providers app.Providers) cli.Command {
	return cli.Command{
		Name:      "deploy",
		Usage:     "Deploy application",
		UsageText: "nullstone deploy <app-name> <env-name> [options]",
		Flags: []cli.Flag{
			cli.StringFlag{
				Name: "stack",
				Usage: `The stack name where the app resides.
       This is only required if multiple apps have the same 'app-name'.`,
			},
			cli.StringFlag{
				Name: "version",
				Usage: `Update the application version.
       app/container: The docker image tag will be set to the version. If a version is not specified, the service will be redeployed with existing configuration.
       app/serverless: The version of the artifact uploaded during 'push'. Version is required to use deploy command.`,
			},
		},
		Action: func(c *cli.Context) error {
			_, cfg, err := SetupProfileCmd(c)
			if err != nil {
				return err
			}

			if c.NArg() != 2 {
				cli.ShowCommandHelp(c, "deploy")
				return fmt.Errorf("invalid usage")
			}
			appName := c.Args().Get(0)
			envName := c.Args().Get(1)
			userConfig := map[string]string{
				"version": c.String("version"),
			}

			finder := NsFinder{Config: cfg}
			app, workspace, err := finder.GetAppAndWorkspace(appName, c.String("stack-name"), envName)
			if err != nil {
				return err
			}

			provider := providers.Find(workspace.Module.Category, workspace.Module.Type)
			if provider == nil {
				return fmt.Errorf("unable to deploy, this CLI does not support category=%s, type=%s", workspace.Module.Category, workspace.Module.Type)
			}
			return provider.Deploy(cfg, app, workspace, userConfig)
		},
	}
}
View Source
var (
	ErrMissingOrg = errors.New("An organization has not been configured with this profile. See 'nullstone set-org -h' for more details.")
)
View Source
var OrgFlag = cli.StringFlag{
	Name:   "org",
	EnvVar: "NULLSTONE_ORG",
	Usage:  `Nullstone organization name used to contextualize API calls. If this flag is not specified, the nullstone CLI will use ~/.nullstone/<profile>/org file.`,
}

OrgFlag defines a flag that the CLI uses

to contextualize API calls by that organization within Nullstone

The organization takes the following precedence:

`--org` flag
`NULLSTONE_ORG` env var
`~/.nullstone/<profile>/org` file
View Source
var ProfileFlag = cli.StringFlag{
	Name:   "profile",
	EnvVar: "NULLSTONE_PROFILE",
	Value:  "default",
	Usage:  "Name of profile",
}
View Source
var Push = func(providers app.Providers) cli.Command {
	return cli.Command{
		Name:      "push",
		Usage:     "Push artifact",
		UsageText: "nullstone push <app-name> <env-name> [options]",
		Flags: []cli.Flag{
			cli.StringFlag{
				Name: "stack",
				Usage: `The stack name where the app resides.
       This is only required if multiple apps have the same 'app-name'.`,
			},
			cli.StringFlag{
				Name: "source",
				Usage: `The source artifact to push.
       app/container: This is the docker image to push. This follows the same syntax as 'docker push NAME[:TAG]'.
       app/serverless: This is a .zip archive to push.`,
				Required: true,
			},
			cli.StringFlag{
				Name: "version",
				Usage: `Push the artifact with this version.
       app/container: If specified, will push the docker image with version as the image tag. Otherwise, uses source tag.
       app/serverless: This is required to upload the artifact.`,
			},
		},
		Action: func(c *cli.Context) error {
			_, cfg, err := SetupProfileCmd(c)
			if err != nil {
				return err
			}

			if c.NArg() != 2 {
				cli.ShowCommandHelp(c, "push")
				return fmt.Errorf("invalid usage")
			}
			appName := c.Args().Get(0)
			envName := c.Args().Get(1)
			userConfig := map[string]string{
				"source":  c.String("source"),
				"version": c.String("version"),
			}

			finder := NsFinder{Config: cfg}
			app, workspace, err := finder.GetAppAndWorkspace(appName, c.String("stack-name"), envName)
			if err != nil {
				return err
			}

			provider := providers.Find(workspace.Module.Category, workspace.Module.Type)
			if provider == nil {
				return fmt.Errorf("unable to push, this CLI does not support category=%s, type=%s", workspace.Module.Category, workspace.Module.Type)
			}

			return provider.Push(cfg, app, workspace, userConfig)
		},
	}
}

Push command performs a docker push to an authenticated image registry configured against an app/container

View Source
var SetOrg = cli.Command{
	Name:  "set-org",
	Usage: "Set the organization for the CLI",
	UsageText: `Most Nullstone CLI commands require a configured nullstone organization to operate.
   This command will set the organization for the current profile.
   If you wish to set the organization per command, use the global --org flag instead.`,
	Flags: []cli.Flag{},
	Action: func(c *cli.Context) error {
		profile, err := config.LoadProfile(GetProfile(c))
		if err != nil {
			return err
		}

		if c.NArg() != 1 {
			return cli.ShowCommandHelp(c, "set-org")
		}

		orgName := c.Args().Get(0)
		if err := profile.SaveOrg(orgName); err != nil {
			return err
		}
		fmt.Fprintf(os.Stderr, "Organization set to %s for %s profile\n", orgName, profile.Name)
		return nil
	},
}

Functions

func GetOrg added in v0.0.4

func GetOrg(c *cli.Context, profile config.Profile) string

func GetProfile added in v0.0.4

func GetProfile(c *cli.Context) string

func SetupProfileCmd added in v0.0.7

func SetupProfileCmd(c *cli.Context) (*config.Profile, api.Config, error)

Types

type ErrMultipleAppsFound added in v0.0.11

type ErrMultipleAppsFound struct {
	AppName    string
	StackNames []string
}

func (ErrMultipleAppsFound) Error added in v0.0.11

func (e ErrMultipleAppsFound) Error() string

type NsFinder added in v0.0.11

type NsFinder struct {
	Config api.Config
}

NsFinder is an object that provides a consistent querying approach for nullstone objects through the CLI It provides nice error messages that are tailored for the user flow of CLI commands

func (NsFinder) GetApp added in v0.0.11

func (f NsFinder) GetApp(appName string, stackName string) (*types.Application, error)

GetApp searches for an app by app name and optionally stack name If only 1 app is found, returns that app If many are found, will return an error with matched app stack names

func (NsFinder) GetAppAndWorkspace added in v0.0.11

func (f NsFinder) GetAppAndWorkspace(appName, stackName, envName string) (*types.Application, *types.Workspace, error)

This retrieves the app and workspace stackName is optional -- If multiple apps are found, this will return an error

func (NsFinder) GetAppWorkspace added in v0.0.18

func (f NsFinder) GetAppWorkspace(app *types.Application, envName string) (*types.Workspace, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL