github.com/fnproject/cli@v0.0.0-20240508150455-e5d88bd86117/objects/app/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 app
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  
    23  	"github.com/fnproject/cli/client"
    24  	"github.com/urfave/cli"
    25  )
    26  
    27  // Create app command
    28  func Create() cli.Command {
    29  	a := appsCmd{}
    30  	return cli.Command{
    31  		Name:     "app",
    32  		Usage:    "Create a new application",
    33  		Category: "MANAGEMENT COMMAND",
    34  		Description: "This command creates a new application.\n	Fn supports grouping functions into a set that defines an application (or API), making it easy to organize and deploy.\n	Applications define a namespace to organize functions and can contain configuration values that are shared across all functions in that application.",
    35  		Aliases: []string{"apps", "a"},
    36  		Before: func(c *cli.Context) error {
    37  			provider, err := client.CurrentProvider()
    38  			if err != nil {
    39  				return err
    40  			}
    41  			a.client = provider.APIClientv2()
    42  			return nil
    43  		},
    44  		ArgsUsage: "<app-name>",
    45  		Action:    a.create,
    46  		Flags: []cli.Flag{
    47  			cli.StringSliceFlag{
    48  				Name:  "config",
    49  				Usage: "Application configuration",
    50  			},
    51  			cli.StringSliceFlag{
    52  				Name:  "annotation",
    53  				Usage: "Application annotations",
    54  			},
    55  			cli.StringFlag{
    56  				Name:  "syslog-url",
    57  				Usage: "Syslog URL to send application logs to",
    58  			},
    59  			cli.StringFlag{
    60  				Name:  "shape",
    61  				Usage: "Valid values are GENERIC_X86, GENERIC_ARM and GENERIC_X86_ARM. Default is GENERIC_X86. Setting this to GENERIC_X86, will run the functions in the application on X86 processor architecture.\n Setting this to GENERIC_ARM, will run the functions in the application on ARM processor architecture.\n When set to 'GENERIC_X86_ARM', functions in the application are run on either X86 or ARM processor architecture.\n Accepted values are:\n GENERIC_X86, GENERIC_ARM, GENERIC_X86_ARM",
    62  			},
    63  		},
    64  	}
    65  }
    66  
    67  // List apps command
    68  func List() cli.Command {
    69  	a := appsCmd{}
    70  	return cli.Command{
    71  		Name:        "apps",
    72  		Usage:       "List all created applications",
    73  		Category:    "MANAGEMENT COMMAND",
    74  		Description: "This command provides a list of defined applications.",
    75  		Aliases:     []string{"app", "a"},
    76  		Before: func(c *cli.Context) error {
    77  			provider, err := client.CurrentProvider()
    78  			if err != nil {
    79  				return err
    80  			}
    81  			a.client = provider.APIClientv2()
    82  			return nil
    83  		},
    84  		Action: a.list,
    85  		Flags: []cli.Flag{
    86  			cli.StringFlag{
    87  				Name:  "cursor",
    88  				Usage: "Pagination cursor",
    89  			},
    90  			cli.Int64Flag{
    91  				Name:  "n",
    92  				Usage: "Number of apps to return",
    93  				Value: int64(100),
    94  			},
    95  			cli.StringFlag{
    96  				Name:  "output",
    97  				Usage: "Output format (json)",
    98  			},
    99  		},
   100  	}
   101  }
   102  
   103  // Delete app command
   104  func Delete() cli.Command {
   105  	a := appsCmd{}
   106  	return cli.Command{
   107  		Name:        "app",
   108  		Usage:       "Delete an application",
   109  		Category:    "MANAGEMENT COMMAND",
   110  		Description: "This command deletes a created application.",
   111  		ArgsUsage:   "<app-name>",
   112  		Aliases:     []string{"apps", "a"},
   113  		Before: func(c *cli.Context) error {
   114  			provider, err := client.CurrentProvider()
   115  			if err != nil {
   116  				return err
   117  			}
   118  			a.client = provider.APIClientv2()
   119  			return nil
   120  		},
   121  		Action: a.delete,
   122  		BashComplete: func(c *cli.Context) {
   123  			args := c.Args()
   124  			if len(args) == 0 {
   125  				BashCompleteApps(c)
   126  			}
   127  		},
   128  		Flags: []cli.Flag{
   129  			cli.BoolFlag{
   130  				Name:  "force, f",
   131  				Usage: "Forces this delete (you will not be asked if you wish to continue with the delete)",
   132  			},
   133  			cli.BoolFlag{
   134  				Name:  "recursive, r",
   135  				Usage: "Delete this app and all associated resources (can fail part way through execution after deleting some resources without the ability to undo)",
   136  			},
   137  		},
   138  	}
   139  }
   140  
   141  // Inspect app command
   142  func Inspect() cli.Command {
   143  	a := appsCmd{}
   144  	return cli.Command{
   145  		Name:        "app",
   146  		Usage:       "Retrieve one or all apps properties",
   147  		Description: "This command inspects properties of an application.",
   148  		Category:    "MANAGEMENT COMMAND",
   149  		Aliases:     []string{"apps", "a"},
   150  		Before: func(c *cli.Context) error {
   151  			provider, err := client.CurrentProvider()
   152  			if err != nil {
   153  				return err
   154  			}
   155  			a.client = provider.APIClientv2()
   156  			return nil
   157  		},
   158  		ArgsUsage: "<app-name> [property.[key]]",
   159  		Action:    a.inspect,
   160  		BashComplete: func(c *cli.Context) {
   161  			switch len(c.Args()) {
   162  			case 0:
   163  				BashCompleteApps(c)
   164  			case 1:
   165  				provider, err := client.CurrentProvider()
   166  				if err != nil {
   167  					return
   168  				}
   169  				app, err := GetAppByName(provider.APIClientv2(), c.Args()[0])
   170  				if err != nil {
   171  					return
   172  				}
   173  				data, err := json.Marshal(app)
   174  				if err != nil {
   175  					return
   176  				}
   177  				var inspect map[string]interface{}
   178  				err = json.Unmarshal(data, &inspect)
   179  				if err != nil {
   180  					return
   181  				}
   182  				for key := range inspect {
   183  					fmt.Println(key)
   184  				}
   185  			}
   186  		},
   187  	}
   188  }
   189  
   190  // Update app command
   191  func Update() cli.Command {
   192  	a := appsCmd{}
   193  	return cli.Command{
   194  		Name:        "app",
   195  		Usage:       "Update an application",
   196  		Category:    "MANAGEMENT COMMAND",
   197  		Description: "This command updates a created application.",
   198  		Aliases:     []string{"apps", "a"},
   199  		Before: func(c *cli.Context) error {
   200  			provider, err := client.CurrentProvider()
   201  			if err != nil {
   202  				return err
   203  			}
   204  			a.client = provider.APIClientv2()
   205  			return nil
   206  		},
   207  		ArgsUsage: "<app-name>",
   208  		Action:    a.update,
   209  		Flags: []cli.Flag{
   210  			cli.StringSliceFlag{
   211  				Name:  "config,c",
   212  				Usage: "Application configuration",
   213  			},
   214  			cli.StringSliceFlag{
   215  				Name:  "annotation",
   216  				Usage: "Application annotations",
   217  			},
   218  			cli.StringFlag{
   219  				Name:  "syslog-url",
   220  				Usage: "Syslog URL to send application logs to",
   221  			},
   222  		},
   223  		BashComplete: func(c *cli.Context) {
   224  			args := c.Args()
   225  			if len(args) == 0 {
   226  				BashCompleteApps(c)
   227  			}
   228  		},
   229  	}
   230  }
   231  
   232  // SetConfig for function command
   233  func SetConfig() cli.Command {
   234  	a := appsCmd{}
   235  	return cli.Command{
   236  		Name:        "app",
   237  		Usage:       "Store a configuration key for this application",
   238  		Description: "This command sets configurations for an application.",
   239  		Category:    "MANAGEMENT COMMAND",
   240  		Aliases:     []string{"apps", "a"},
   241  		Before: func(c *cli.Context) error {
   242  			provider, err := client.CurrentProvider()
   243  			if err != nil {
   244  				return err
   245  			}
   246  			a.client = provider.APIClientv2()
   247  			return nil
   248  		},
   249  		ArgsUsage: "<app-name> <key> <value>",
   250  		Action:    a.setConfig,
   251  		BashComplete: func(c *cli.Context) {
   252  			args := c.Args()
   253  			if len(args) == 0 {
   254  				BashCompleteApps(c)
   255  			}
   256  		},
   257  	}
   258  }
   259  
   260  // ListConfig for app command
   261  func ListConfig() cli.Command {
   262  	a := appsCmd{}
   263  	return cli.Command{
   264  		Name:        "app",
   265  		Usage:       "List configuration key/value pairs for this application",
   266  		Category:    "MANAGEMENT COMMAND",
   267  		Description: "This command lists the configuration of an application.",
   268  		Aliases:     []string{"apps", "a"},
   269  		Before: func(c *cli.Context) error {
   270  			provider, err := client.CurrentProvider()
   271  			if err != nil {
   272  				return err
   273  			}
   274  			a.client = provider.APIClientv2()
   275  			return nil
   276  		},
   277  		ArgsUsage: "<app-name>",
   278  		Action:    a.listConfig,
   279  		BashComplete: func(c *cli.Context) {
   280  			args := c.Args()
   281  			if len(args) == 0 {
   282  				BashCompleteApps(c)
   283  			}
   284  		},
   285  	}
   286  }
   287  
   288  // GetConfig for function command
   289  func GetConfig() cli.Command {
   290  	a := appsCmd{}
   291  	return cli.Command{
   292  		Name:        "app",
   293  		Usage:       "Inspect configuration key for this application",
   294  		Description: "This command gets the configuration of an application.",
   295  		Category:    "MANAGEMENT COMMAND",
   296  		Aliases:     []string{"apps", "a"},
   297  		Before: func(c *cli.Context) error {
   298  			provider, err := client.CurrentProvider()
   299  			if err != nil {
   300  				return err
   301  			}
   302  			a.client = provider.APIClientv2()
   303  			return nil
   304  		},
   305  		ArgsUsage: "<app-name> <key>",
   306  		Action:    a.getConfig,
   307  		BashComplete: func(c *cli.Context) {
   308  			switch len(c.Args()) {
   309  			case 0:
   310  				BashCompleteApps(c)
   311  			case 1:
   312  				provider, err := client.CurrentProvider()
   313  				if err != nil {
   314  					return
   315  				}
   316  				app, err := GetAppByName(provider.APIClientv2(), c.Args()[0])
   317  				if err != nil {
   318  					return
   319  				}
   320  				for key := range app.Config {
   321  					fmt.Println(key)
   322  				}
   323  			}
   324  		},
   325  	}
   326  }
   327  
   328  // UnsetConfig for app command
   329  func UnsetConfig() cli.Command {
   330  	a := appsCmd{}
   331  	return cli.Command{
   332  		Name:        "app",
   333  		Usage:       "Remove a configuration key for this application.",
   334  		Description: "This command removes a configuration for an application.",
   335  		Category:    "MANAGEMENT COMMAND",
   336  		Aliases:     []string{"apps", "a"},
   337  		Before: func(c *cli.Context) error {
   338  			provider, err := client.CurrentProvider()
   339  			if err != nil {
   340  				return err
   341  			}
   342  			a.client = provider.APIClientv2()
   343  			return nil
   344  		},
   345  		ArgsUsage: "<app-name> <key>",
   346  		Action:    a.unsetConfig,
   347  		BashComplete: func(c *cli.Context) {
   348  			switch len(c.Args()) {
   349  			case 0:
   350  				BashCompleteApps(c)
   351  			case 1:
   352  				provider, err := client.CurrentProvider()
   353  				if err != nil {
   354  					return
   355  				}
   356  				app, err := GetAppByName(provider.APIClientv2(), c.Args()[0])
   357  				if err != nil {
   358  					return
   359  				}
   360  				for key := range app.Config {
   361  					fmt.Println(key)
   362  				}
   363  			}
   364  		},
   365  	}
   366  }