github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/worker/uniter/runner/jujuc/k8s-raw-set.go (about) 1 // Copyright 2020 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package jujuc 5 6 import ( 7 "github.com/juju/cmd/v3" 8 "github.com/juju/errors" 9 "github.com/juju/gnuflag" 10 11 jujucmd "github.com/juju/juju/cmd" 12 ) 13 14 // K8sRawSetCommand implements the k8s-raw-set command. 15 type K8sRawSetCommand struct { 16 cmd.CommandBase 17 ctx Context 18 19 specFile cmd.FileVar 20 } 21 22 // NewK8sRawSetCommand makes a k8s-raw-set command. 23 func NewK8sRawSetCommand(ctx Context) (cmd.Command, error) { 24 return &K8sRawSetCommand{ctx: ctx}, nil 25 } 26 27 func (c *K8sRawSetCommand) Info() *cmd.Info { 28 doc := ` 29 Sets configuration data in k8s raw format to use for k8s resources. 30 The spec applies to all units for the application. 31 ` 32 return jujucmd.Info(&cmd.Info{ 33 Name: "k8s-raw-set", 34 Args: "--file <core spec file>", 35 Purpose: "set k8s raw spec information", 36 Doc: doc, 37 }) 38 } 39 40 func (c *K8sRawSetCommand) SetFlags(f *gnuflag.FlagSet) { 41 c.specFile.SetStdin() 42 c.specFile.Path = "-" 43 f.Var(&c.specFile, "file", "file containing k8s raw spec") 44 } 45 46 func (c *K8sRawSetCommand) Init(args []string) error { 47 return cmd.CheckEmpty(args) 48 } 49 50 func (c *K8sRawSetCommand) Run(ctx *cmd.Context) error { 51 specData, err := c.handleSpecFile(ctx) 52 if err != nil { 53 return errors.Trace(err) 54 } 55 return c.ctx.SetRawK8sSpec(specData) 56 } 57 58 func (c *K8sRawSetCommand) handleSpecFile(ctx *cmd.Context) (string, error) { 59 specData, err := c.specFile.Read(ctx) 60 if err != nil { 61 return "", errors.Trace(err) 62 } 63 if len(specData) == 0 { 64 return "", errors.New("no k8s raw spec specified: pipe k8s raw spec to command, or specify a file with --file") 65 } 66 return string(specData), nil 67 }