-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wanna develop a new CDK component? Run the command: ``` lacework component dev <component_name> ``` Signed-off-by: Salim Afiune Maya <[email protected]>
- Loading branch information
Showing
2 changed files
with
178 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,9 @@ package cmd | |
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/AlecAivazis/survey/v2" | ||
"github.com/fatih/color" | ||
"github.com/olekukonko/tablewriter" | ||
"github.com/pkg/errors" | ||
|
@@ -76,6 +78,15 @@ var ( | |
Args: cobra.ExactArgs(1), | ||
RunE: runComponentsDelete, | ||
} | ||
|
||
// componentsDevModeCmd represents the dev sub-command inside the components command | ||
componentsDevModeCmd = &cobra.Command{ | ||
Use: "dev <component>", | ||
Hidden: true, | ||
Short: "Enter development mode of a new or existing component", | ||
Args: cobra.ExactArgs(1), | ||
RunE: runComponentsDevMode, | ||
} | ||
) | ||
|
||
func init() { | ||
|
@@ -87,6 +98,7 @@ func init() { | |
componentsCmd.AddCommand(componentsInstallCmd) | ||
componentsCmd.AddCommand(componentsUpdateCmd) | ||
componentsCmd.AddCommand(componentsUninstallCmd) | ||
componentsCmd.AddCommand(componentsDevModeCmd) | ||
|
||
// load components dynamically | ||
cli.LoadComponents() | ||
|
@@ -360,7 +372,10 @@ func runComponentsDelete(_ *cobra.Command, args []string) (err error) { | |
return | ||
} | ||
|
||
if !component.IsInstalled() { | ||
if component.UnderDevelopment() { | ||
cli.OutputHuman("Component '%s' in under development. Bypassing checks.\n\n", | ||
color.HiYellowString(component.Name)) | ||
} else if !component.IsInstalled() { | ||
err = errors.Errorf( | ||
"component not installed. Try running 'lacework component install %s'", | ||
args[0], | ||
|
@@ -404,3 +419,74 @@ func runComponentsDelete(_ *cobra.Command, args []string) (err error) { | |
cli.OutputHuman("Reach out to us at %s\n", color.HiCyanString("[email protected]")) | ||
return | ||
} | ||
|
||
func runComponentsDevMode(_ *cobra.Command, args []string) error { | ||
cli.StartProgress("Loading components state...") | ||
var err error | ||
cli.LwComponents, err = lwcomponent.LoadState(cli.LwApi) | ||
cli.StopProgress() | ||
if err != nil { | ||
return errors.Wrap(err, "unable to load components") | ||
} | ||
|
||
component, found := cli.LwComponents.GetComponent(args[0]) | ||
if !found { | ||
component = &lwcomponent.Component{ | ||
Name: args[0], | ||
} | ||
|
||
if component.UnderDevelopment() { | ||
return errors.New("component already under development.") | ||
} | ||
|
||
cli.OutputHuman("Component '%s' not found. Defining a new component.\n", | ||
color.HiYellowString(component.Name)) | ||
|
||
var ( | ||
cType string | ||
helpMsg = fmt.Sprintf("What are these component types ?\n"+ | ||
"\n'%s' - A regular standalone-binary (this component type is not accessible via the CLI)"+ | ||
"\n'%s' - A binary accessible via the Lacework CLI (Users will run 'lacework <COMPONENT_NAME>')"+ | ||
"\n'%s' - A library that only provides content for the CLI or other components\n", | ||
lwcomponent.BinaryType, lwcomponent.CommandType, lwcomponent.LibraryType) | ||
) | ||
if err := survey.AskOne(&survey.Select{ | ||
Message: "Select the type of component you are developing:", | ||
Help: helpMsg, | ||
Options: []string{ | ||
lwcomponent.BinaryType, | ||
lwcomponent.CommandType, | ||
lwcomponent.LibraryType, | ||
}, | ||
}, &cType); err != nil { | ||
return err | ||
} | ||
|
||
component.Type = lwcomponent.Type(cType) | ||
|
||
if err := survey.AskOne(&survey.Input{ | ||
Message: "What is this component about? (component description):", | ||
}, &component.Description); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if err := component.EnterDevelopmentMode(); err != nil { | ||
return errors.Wrap(err, "unable to enter development mode") | ||
} | ||
|
||
rPath, err := component.RootPath() | ||
if err != nil { | ||
return errors.New("unable to detect RootPath") | ||
} | ||
|
||
cli.OutputHuman("Component '%s' in now in development mode.\n\n", | ||
color.HiYellowString(component.Name)) | ||
cli.OutputHuman("Root path: %s\n", rPath) | ||
cli.OutputHuman("Dev specs: %s\n", filepath.Join(rPath, ".dev")) | ||
if component.Type == lwcomponent.CommandType { | ||
cli.OutputHuman("\nDeploy your dev component at: %s\n", | ||
color.HiYellowString(filepath.Join(rPath, component.Name))) | ||
} | ||
return nil | ||
} |
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