github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/compute/metadata.go (about)

     1  package compute
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	"github.com/fastly/cli/pkg/argparser"
     8  	"github.com/fastly/cli/pkg/config"
     9  	fsterr "github.com/fastly/cli/pkg/errors"
    10  	"github.com/fastly/cli/pkg/global"
    11  	"github.com/fastly/cli/pkg/text"
    12  )
    13  
    14  // MetadataCommand controls what metadata is collected for a Wasm binary.
    15  type MetadataCommand struct {
    16  	argparser.Base
    17  
    18  	disable        bool
    19  	disableBuild   bool
    20  	disableMachine bool
    21  	disablePackage bool
    22  	disableScript  bool
    23  	enable         bool
    24  	enableBuild    bool
    25  	enableMachine  bool
    26  	enablePackage  bool
    27  	enableScript   bool
    28  }
    29  
    30  // NewMetadataCommand returns a new command registered in the parent.
    31  func NewMetadataCommand(parent argparser.Registerer, g *global.Data) *MetadataCommand {
    32  	var c MetadataCommand
    33  	c.Globals = g
    34  	c.CmdClause = parent.Command("metadata", "Control what metadata is collected")
    35  	c.CmdClause.Flag("disable", "Disable all metadata").BoolVar(&c.disable)
    36  	c.CmdClause.Flag("disable-build", "Disable metadata for information regarding the time taken for builds and compilation processes").BoolVar(&c.disableBuild)
    37  	c.CmdClause.Flag("disable-machine", "Disable metadata for general, non-identifying system specifications (CPU, RAM, operating system)").BoolVar(&c.disableMachine)
    38  	c.CmdClause.Flag("disable-package", "Disable metadata for packages and libraries utilized in your source code").BoolVar(&c.disablePackage)
    39  	c.CmdClause.Flag("disable-script", "Disable metadata for script info from the fastly.toml manifest (i.e. [scripts] section).").BoolVar(&c.disableScript)
    40  	c.CmdClause.Flag("enable", "Enable all metadata").BoolVar(&c.enable)
    41  	c.CmdClause.Flag("enable-build", "Enable metadata for information regarding the time taken for builds and compilation processes").BoolVar(&c.enableBuild)
    42  	c.CmdClause.Flag("enable-machine", "Enable metadata for general, non-identifying system specifications (CPU, RAM, operating system)").BoolVar(&c.enableMachine)
    43  	c.CmdClause.Flag("enable-package", "Enable metadata for packages and libraries utilized in your source code").BoolVar(&c.enablePackage)
    44  	c.CmdClause.Flag("enable-script", "Enable metadata for script info from the fastly.toml manifest (i.e. [scripts] section).").BoolVar(&c.enableScript)
    45  	return &c
    46  }
    47  
    48  // Exec implements the command interface.
    49  func (c *MetadataCommand) Exec(_ io.Reader, out io.Writer) error {
    50  	if c.disable && c.enable {
    51  		return fsterr.ErrInvalidEnableDisableFlagCombo
    52  	}
    53  
    54  	var modified bool
    55  
    56  	// Global enable/disable
    57  	if c.enable {
    58  		c.Globals.Config.WasmMetadata = toggleAll("enable")
    59  		modified = true
    60  	}
    61  	if c.disable {
    62  		c.Globals.Config.WasmMetadata = toggleAll("disable")
    63  		modified = true
    64  	}
    65  
    66  	// Specific enablement
    67  	if c.enableBuild {
    68  		c.Globals.Config.WasmMetadata.BuildInfo = "enable"
    69  		modified = true
    70  	}
    71  	if c.enableMachine {
    72  		c.Globals.Config.WasmMetadata.MachineInfo = "enable"
    73  		modified = true
    74  	}
    75  	if c.enablePackage {
    76  		c.Globals.Config.WasmMetadata.PackageInfo = "enable"
    77  		modified = true
    78  	}
    79  	if c.enableScript {
    80  		c.Globals.Config.WasmMetadata.ScriptInfo = "enable"
    81  		modified = true
    82  	}
    83  
    84  	// Specific disablement
    85  	if c.disableBuild {
    86  		c.Globals.Config.WasmMetadata.BuildInfo = "disable"
    87  		modified = true
    88  	}
    89  	if c.disableMachine {
    90  		c.Globals.Config.WasmMetadata.MachineInfo = "disable"
    91  		modified = true
    92  	}
    93  	if c.disablePackage {
    94  		c.Globals.Config.WasmMetadata.PackageInfo = "disable"
    95  		modified = true
    96  	}
    97  	if c.disableScript {
    98  		c.Globals.Config.WasmMetadata.ScriptInfo = "disable"
    99  		modified = true
   100  	}
   101  
   102  	if modified {
   103  		if c.disable && (c.enableBuild || c.enableMachine || c.enablePackage || c.enableScript) {
   104  			text.Info(out, "We will disable all metadata except for the specified `--enable-*` flags")
   105  			text.Break(out)
   106  		}
   107  		if c.enable && (c.disableBuild || c.disableMachine || c.disablePackage || c.disableScript) {
   108  			text.Info(out, "We will enable all metadata except for the specified `--disable-*` flags")
   109  			text.Break(out)
   110  		}
   111  		err := c.Globals.Config.Write(c.Globals.ConfigPath)
   112  		if err != nil {
   113  			return fmt.Errorf("failed to persist metadata choices to disk: %w", err)
   114  		}
   115  		text.Success(out, "configuration updated")
   116  		text.Break(out)
   117  	}
   118  
   119  	text.Output(out, "Build Information: %s", c.Globals.Config.WasmMetadata.BuildInfo)
   120  	text.Output(out, "Machine Information: %s", c.Globals.Config.WasmMetadata.MachineInfo)
   121  	text.Output(out, "Package Information: %s", c.Globals.Config.WasmMetadata.PackageInfo)
   122  	text.Output(out, "Script Information: %s", c.Globals.Config.WasmMetadata.ScriptInfo)
   123  	return nil
   124  }
   125  
   126  func toggleAll(state string) config.WasmMetadata {
   127  	var t config.WasmMetadata
   128  	t.BuildInfo = state
   129  	t.MachineInfo = state
   130  	t.PackageInfo = state
   131  	t.ScriptInfo = state
   132  	return t
   133  }