go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/lucicfg/cli/app.go (about) 1 // Copyright 2018 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package cli contains command line interface for lucicfg tool. 16 package cli 17 18 import ( 19 "context" 20 "os" 21 22 "github.com/maruel/subcommands" 23 "go.starlark.net/resolve" 24 "gopkg.in/yaml.v2" 25 26 "go.chromium.org/luci/auth/client/authcli" 27 "go.chromium.org/luci/client/versioncli" 28 "go.chromium.org/luci/common/cli" 29 "go.chromium.org/luci/common/flag/fixflagpos" 30 "go.chromium.org/luci/common/logging/gologger" 31 32 "go.chromium.org/luci/lucicfg" 33 "go.chromium.org/luci/lucicfg/cli/base" 34 "go.chromium.org/luci/lucicfg/cli/cmds/fmt" 35 "go.chromium.org/luci/lucicfg/cli/cmds/generate" 36 "go.chromium.org/luci/lucicfg/cli/cmds/lint" 37 "go.chromium.org/luci/lucicfg/cli/cmds/validate" 38 ) 39 40 // Main runs the lucicfg CLI. 41 func Main(params base.Parameters, args []string) int { 42 // Enable not-yet-standard Starlark features. 43 resolve.AllowLambda = true 44 resolve.AllowNestedDef = true 45 resolve.AllowFloat = true 46 resolve.AllowSet = true 47 48 // We prefer not to wrap lines in generated YAML. 49 yaml.FutureLineWrap() 50 51 // A hack to allow '#!/usr/bin/env lucicfg'. On Linux the shebang line can 52 // have at most two arguments, so we can't use '#!/usr/bin/env lucicfg gen'. 53 if len(args) == 1 { 54 if _, err := os.Stat(args[0]); err == nil { 55 args = []string{"generate", args[0]} 56 } 57 } 58 59 return subcommands.Run(GetApplication(params), fixflagpos.FixSubcommands(args)) 60 } 61 62 // GetApplication returns lucicfg cli.Application. 63 func GetApplication(params base.Parameters) *cli.Application { 64 return &cli.Application{ 65 Name: "lucicfg", 66 Title: "LUCI config generator (" + lucicfg.UserAgent + ")", 67 68 Context: func(ctx context.Context) context.Context { 69 loggerConfig := gologger.LoggerConfig{ 70 Format: `[P%{pid} %{time:15:04:05.000} %{shortfile} %{level:.1s}] %{message}`, 71 Out: os.Stderr, 72 } 73 return loggerConfig.Use(ctx) 74 }, 75 76 Commands: []*subcommands.Command{ 77 subcommands.Section("Config generation\n"), 78 generate.Cmd(params), 79 validate.Cmd(params), 80 fmt.Cmd(params), 81 lint.Cmd(params), 82 83 subcommands.Section("Authentication for LUCI Config\n"), 84 authcli.SubcommandInfo(params.AuthOptions, "auth-info", true), 85 authcli.SubcommandLogin(params.AuthOptions, "auth-login", false), 86 authcli.SubcommandLogout(params.AuthOptions, "auth-logout", false), 87 88 subcommands.Section("Misc\n"), 89 subcommands.CmdHelp, 90 versioncli.CmdVersion(lucicfg.UserAgent), 91 }, 92 } 93 }