mirror of
https://github.com/outbackdingo/certificates.git
synced 2026-01-27 10:18:34 +00:00
Create context for automatic RAs
It creates a new context with the given name if the flags --token and --context are passed, and the context does not exist. Fixes #1047
This commit is contained in:
@@ -121,7 +121,13 @@ func appAction(ctx *cli.Context) error {
|
||||
|
||||
// Allow custom contexts.
|
||||
if caCtx := ctx.String("context"); caCtx != "" {
|
||||
if err := step.Contexts().SetCurrent(caCtx); err != nil {
|
||||
if _, ok := step.Contexts().Get(caCtx); ok {
|
||||
if err := step.Contexts().SetCurrent(caCtx); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if token == "" {
|
||||
return fmt.Errorf("context %q not found", caCtx)
|
||||
} else if err := createContext(caCtx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -234,6 +240,26 @@ To get a linked authority token:
|
||||
return nil
|
||||
}
|
||||
|
||||
// createContext creates a new context using the given name for the context,
|
||||
// authority and profile.
|
||||
func createContext(name string) error {
|
||||
if err := step.Contexts().Add(&step.Context{
|
||||
Name: name, Authority: name, Profile: name,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("error adding context: %w", err)
|
||||
}
|
||||
if err := step.Contexts().SaveCurrent(name); err != nil {
|
||||
return fmt.Errorf("error saving context: %w", err)
|
||||
}
|
||||
if err := step.Contexts().SetCurrent(name); err != nil {
|
||||
return fmt.Errorf("error setting context: %w", err)
|
||||
}
|
||||
if err := os.MkdirAll(step.Path(), 0700); err != nil {
|
||||
return fmt.Errorf("error creating directory: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// fatal writes the passed error on the standard error and exits with the exit
|
||||
// code 1. If the environment variable STEPDEBUG is set to 1 it shows the
|
||||
// stack trace of the error.
|
||||
|
||||
Reference in New Issue
Block a user