github.com/fnproject/cli@v0.0.0-20240508150455-e5d88bd86117/objects/context/commands.go (about)

     1  /*
     2   * Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package context
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/urfave/cli"
    23  )
    24  
    25  // Create context command
    26  func Create() cli.Command {
    27  	return cli.Command{
    28  		Name:        "context",
    29  		Usage:       "Create a new context",
    30  		Aliases:     []string{"ctx"},
    31  		ArgsUsage:   "<context-name>",
    32  		Category:    "MANAGEMENT COMMAND",
    33  		Description: "This command creates a new context for a created application.",
    34  		Action:      createCtx,
    35  		Flags: []cli.Flag{
    36  			cli.StringFlag{
    37  				Name:  "provider",
    38  				Usage: "Context provider",
    39  			},
    40  			cli.StringFlag{
    41  				Name:  "api-url",
    42  				Usage: "Context api url",
    43  			},
    44  			cli.StringFlag{
    45  				Name:  "registry",
    46  				Usage: "Context registry",
    47  			},
    48  		},
    49  	}
    50  }
    51  
    52  // List contexts command
    53  func List() cli.Command {
    54  	return cli.Command{
    55  		Name:        "contexts",
    56  		Usage:       "List contexts",
    57  		Aliases:     []string{"context", "ctx"},
    58  		Category:    "MANAGEMENT COMMAND",
    59  		Description: "This command returns a list of contexts.",
    60  		Action:      listCtx,
    61  		Flags: []cli.Flag{
    62  			cli.StringFlag{
    63  				Name:  "output",
    64  				Usage: "Output format (json)",
    65  				Value: "",
    66  			},
    67  		},
    68  	}
    69  }
    70  
    71  // Delete context command
    72  func Delete() cli.Command {
    73  	return cli.Command{
    74  		Name:        "context",
    75  		Usage:       "Delete a context",
    76  		Aliases:     []string{"ctx"},
    77  		ArgsUsage:   "<context-name>",
    78  		Description: "This command deletes a context.",
    79  		Category:    "MANAGEMENT COMMAND",
    80  		Action:      deleteCtx,
    81  		BashComplete: func(c *cli.Context) {
    82  			switch len(c.Args()) {
    83  			case 0:
    84  				contexts, err := getAvailableContexts()
    85  				if err != nil {
    86  					return
    87  				}
    88  				for _, c := range contexts {
    89  					if c.Name != "default" {
    90  						fmt.Println(c.Name)
    91  					}
    92  				}
    93  			}
    94  		},
    95  	}
    96  }
    97  
    98  // Inspect context command
    99  func Inspect() cli.Command {
   100  	return cli.Command{
   101  		Name:     "context",
   102  		Usage:    "Inspect the contents of a context, if no context is specified the current-context will be used.",
   103  		Aliases:  []string{"ctx"},
   104  		Category: "MANAGEMENT COMMAND",
   105  		Action:   inspectCtx,
   106  		BashComplete: func(c *cli.Context) {
   107  			switch len(c.Args()) {
   108  			case 0:
   109  				contexts, err := getAvailableContexts()
   110  				if err != nil {
   111  					return
   112  				}
   113  				for _, c := range contexts {
   114  					fmt.Println(c.Name)
   115  				}
   116  			}
   117  		},
   118  	}
   119  }
   120  
   121  // Update context command
   122  func Update() cli.Command {
   123  	ctxMap := ContextMap{}
   124  	return cli.Command{
   125  		Name:        "context",
   126  		Usage:       "Update context files",
   127  		Aliases:     []string{"ctx"},
   128  		ArgsUsage:   "<key> [value]",
   129  		Category:    "MANAGEMENT COMMAND",
   130  		Description: "This command updates the current context file.",
   131  		Action:      ctxMap.updateCtx,
   132  		Flags: []cli.Flag{
   133  			cli.BoolFlag{
   134  				Name:  "delete",
   135  				Usage: "Delete key=value pair from context file.",
   136  			},
   137  		},
   138  	}
   139  }
   140  
   141  // Use context command
   142  func Use() cli.Command {
   143  	return cli.Command{
   144  		Name:        "context",
   145  		Usage:       "Use context for future invocations",
   146  		Aliases:     []string{"ctx"},
   147  		ArgsUsage:   "<context-name>",
   148  		Category:    "MANAGEMENT COMMAND",
   149  		Description: "This command uses context for future invocations.",
   150  		Action:      useCtx,
   151  		BashComplete: func(c *cli.Context) {
   152  			switch len(c.Args()) {
   153  			case 0:
   154  				contexts, err := getAvailableContexts()
   155  				if err != nil {
   156  					return
   157  				}
   158  				for _, c := range contexts {
   159  					fmt.Println(c.Name)
   160  				}
   161  			}
   162  		},
   163  	}
   164  }
   165  
   166  // Unset context command
   167  func Unset() cli.Command {
   168  	return cli.Command{
   169  		Name:        "context",
   170  		Usage:       "Unset current-context",
   171  		Aliases:     []string{"ctx"},
   172  		Category:    "MANAGEMENT COMMAND",
   173  		Description: "This command unsets the current context in use.",
   174  		Action:      unsetCtx,
   175  	}
   176  }