github.com/pluralsh/plural-cli@v0.9.5/cmd/plural/cd_contexts.go (about)

     1  package plural
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	gqlclient "github.com/pluralsh/console-client-go"
     8  	"github.com/pluralsh/plural-cli/pkg/console"
     9  	"github.com/pluralsh/plural-cli/pkg/utils"
    10  	"github.com/urfave/cli"
    11  )
    12  
    13  func (p *Plural) cdContexts() cli.Command {
    14  	return cli.Command{
    15  		Name:        "contexts",
    16  		Subcommands: p.cdServiceContextCommands(),
    17  		Usage:       "manage CD service contexts",
    18  	}
    19  }
    20  
    21  func (p *Plural) cdServiceContextCommands() []cli.Command {
    22  	return []cli.Command{
    23  		{
    24  			Name:      "upsert",
    25  			ArgsUsage: "NAME",
    26  			Flags: []cli.Flag{
    27  				cli.StringFlag{Name: "config-file", Usage: "path for json configuration file with the context blob", Required: true},
    28  				cli.StringFlag{Name: "name", Usage: "context name", Required: true},
    29  			},
    30  			Action: latestVersion(requireArgs(p.handleUpsertServiceContext, []string{"NAME"})),
    31  			Usage:  "upsert service context",
    32  		},
    33  		{
    34  			Name:      "get",
    35  			ArgsUsage: "NAME",
    36  			Action:    latestVersion(requireArgs(p.handleGetServiceContext, []string{"NAME"})),
    37  			Usage:     "get service context",
    38  		},
    39  	}
    40  }
    41  
    42  func (p *Plural) handleUpsertServiceContext(c *cli.Context) error {
    43  	if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil {
    44  		return err
    45  	}
    46  
    47  	contextName := c.String("name")
    48  	serviceContextName := c.Args().Get(0)
    49  	attributes := gqlclient.ServiceContextAttributes{}
    50  
    51  	configFile, err := utils.ReadFile(c.String("config-file"))
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	// validate
    57  	conf := map[string]interface{}{}
    58  	if err := json.Unmarshal([]byte(configFile), &conf); err != nil {
    59  		return err
    60  	}
    61  
    62  	configuration := map[string]map[string]interface{}{}
    63  	configuration[contextName] = conf
    64  
    65  	configurationJson, err := json.Marshal(configuration)
    66  	if err != nil {
    67  		return err
    68  	}
    69  	configurationJsonString := string(configurationJson)
    70  	attributes.Configuration = &configurationJsonString
    71  
    72  	sc, err := p.ConsoleClient.SaveServiceContext(serviceContextName, attributes)
    73  	if err != nil {
    74  		return err
    75  	}
    76  	if sc == nil {
    77  		return fmt.Errorf("the returned object is empty, check if all fields are set")
    78  	}
    79  
    80  	desc, err := console.DescribeServiceContext(sc)
    81  	if err != nil {
    82  		return err
    83  	}
    84  	fmt.Print(desc)
    85  	return nil
    86  }
    87  
    88  func (p *Plural) handleGetServiceContext(c *cli.Context) error {
    89  	if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil {
    90  		return err
    91  	}
    92  
    93  	contextName := c.Args().Get(0)
    94  
    95  	sc, err := p.ConsoleClient.GetServiceContext(contextName)
    96  	if err != nil {
    97  		return err
    98  	}
    99  	if sc == nil {
   100  		return fmt.Errorf("the returned object is empty, check if all fields are set")
   101  	}
   102  
   103  	desc, err := console.DescribeServiceContext(sc)
   104  	if err != nil {
   105  		return err
   106  	}
   107  	fmt.Print(desc)
   108  	return nil
   109  }