fixtures

package
v0.15.14 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2024 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Command = cli.Command{
	Name:        "fixtures",
	Description: "Set up fixtures for testing Common Fate providers",
	Subcommands: []*cli.Command{&CreateCommand, &DestroyCommand},
	Action:      cli.ShowSubcommandHelp,
}
View Source
var CreateCommand = cli.Command{
	Name: "create",
	Flags: []cli.Flag{
		&cli.PathFlag{Name: "path", Value: "fixtures", Usage: "The path to the fixture JSON file to read or write to"},
		&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Usage: "The name of the provider to generate fixtures for", Required: true},
	},
	Action: func(c *cli.Context) error {
		ctx := c.Context

		_ = godotenv.Load()

		name := c.String("name")
		g, err := LookupGenerator(name)
		if err != nil {
			return err
		}

		parentFolder := c.Path("path")

		fixturePath := filepath.Join(parentFolder, name+".json")
		if _, err := os.Stat(fixturePath); err == nil {
			return fmt.Errorf("fixture already exists (%s). Use 'gdk fixtures delete --name %s' to remove it before generating it again", fixturePath, name)
		}

		ac := deploy.EnvDeploymentConfig{}
		pc, err := ac.ReadProviders(ctx)
		if err != nil {
			return errors.Wrap(err, "reading providers")
		}

		if configer, ok := g.(gconfig.Configer); ok {
			p := pc[name]
			err = configer.Config().Load(ctx, &gconfig.MapLoader{Values: p.With})
			if err != nil {
				return errors.Wrap(err, "loading config")
			}
		}

		if configurer, ok := g.(gconfig.Initer); ok {
			err = configurer.Init(ctx)
			if err != nil {
				return err
			}
		}

		fixtures, err := g.Generate(ctx)
		if err != nil {
			return err
		}

		err = os.MkdirAll(parentFolder, os.ModePerm)
		if err != nil {
			return err
		}

		err = os.WriteFile(fixturePath, fixtures, 0666)
		if err != nil {
			return err
		}

		zap.S().Infow("created fixture", "file", fixturePath)

		return nil
	},
}
View Source
var DestroyCommand = cli.Command{
	Name: "destroy",
	Flags: []cli.Flag{
		&cli.PathFlag{Name: "path", Value: "fixtures", Usage: "The path to the fixture JSON file to read or write to"},
		&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Usage: "The name of the provider to generate fixtures for", Required: true},
	},
	Action: func(c *cli.Context) error {
		_ = godotenv.Load()

		ctx := c.Context

		name := c.String("name")
		g, err := LookupGenerator(name)
		if err != nil {
			return err
		}
		ac := deploy.EnvDeploymentConfig{}
		pc, err := ac.ReadProviders(ctx)
		if err != nil {
			return err
		}

		if configer, ok := g.(gconfig.Configer); ok {
			p := pc[name]
			err = configer.Config().Load(ctx, &gconfig.MapLoader{Values: p.With})
			if err != nil {
				return err
			}
		}

		if configurer, ok := g.(gconfig.Initer); ok {
			err = configurer.Init(ctx)
			if err != nil {
				return err
			}
		}

		p := c.Path("path")
		fixturePath := filepath.Join(p, name+".json")

		data, err := os.ReadFile(fixturePath)
		if err != nil {
			return err
		}

		err = g.Destroy(ctx, data)
		if err != nil {
			return err
		}

		err = os.Remove(fixturePath)
		if err != nil {
			return err
		}

		zap.S().Infow("destroyed fixture", "file", fixturePath)

		return nil
	},
}
View Source
var FixtureRegistry = map[string]GeneratorDestroyer{
	"aws-sso-v2": &ssofv2.Generator{},
	"okta":       &oktaf.Generator{},
	"azure":      &adf.Generator{},
}

Functions

This section is empty.

Types

type GeneratorDestroyer

type GeneratorDestroyer interface {
	// Generate the fixture data. Returns a JSON encoding of the fixture data.
	Generate(ctx context.Context) ([]byte, error)
	// Destroy the fixture. 'data' is the JSON encoded fixture data.
	Destroy(ctx context.Context, data []byte) error
}

func LookupGenerator

func LookupGenerator(name string) (GeneratorDestroyer, error)

Jump to

Keyboard shortcuts

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