github.com/greenpau/go-authcrunch@v1.1.4/cmd/authdbctl/main.go (about)

     1  // Copyright 2022 Paul Greenberg greenpau@outlook.com
     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 main
    16  
    17  import (
    18  	"fmt"
    19  	"github.com/urfave/cli/v2"
    20  	"log"
    21  	"os"
    22  
    23  	"github.com/greenpau/versioned"
    24  )
    25  
    26  var (
    27  	app        *versioned.PackageManager
    28  	appVersion string
    29  	gitBranch  string
    30  	gitCommit  string
    31  	buildUser  string
    32  	buildDate  string
    33  	sh         *cli.App
    34  )
    35  
    36  func init() {
    37  	app = versioned.NewPackageManager("authdbctl")
    38  	app.Description = "AuthDB management client"
    39  	app.Documentation = "https://github.com/greenpau/go-authcrunch/"
    40  	app.SetVersion(appVersion, "1.1.4")
    41  	app.SetGitBranch(gitBranch, "main")
    42  	app.SetGitCommit(gitCommit, "v1.1.3-5-gfc7ea81")
    43  	app.SetBuildUser(buildUser, "")
    44  	app.SetBuildDate(buildDate, "")
    45  
    46  	cli.VersionPrinter = func(c *cli.Context) {
    47  		fmt.Fprintf(os.Stdout, "%s\n", app.Banner())
    48  	}
    49  
    50  	sh = cli.NewApp()
    51  	sh.Name = app.Name
    52  	sh.Version = app.Version
    53  	sh.Usage = app.Description
    54  	sh.Description = app.Documentation
    55  	sh.HideHelp = false
    56  	sh.HideVersion = false
    57  	sh.Flags = append(sh.Flags, &cli.StringFlag{
    58  		Name:        "config",
    59  		Aliases:     []string{"c"},
    60  		Usage:       "Sets `PATH` to configuration file",
    61  		Value:       `~/.config/authdbctl/config.yaml`,
    62  		DefaultText: `~/.config/authdbctl/config.yaml`,
    63  		EnvVars:     []string{"AUTHDBCTL_CONFIG_PATH"},
    64  	})
    65  	sh.Flags = append(sh.Flags, &cli.StringFlag{
    66  		Name:        "token-path",
    67  		Usage:       "Sets `PATH` to token file",
    68  		Value:       `~/.config/authdbctl/token.jwt`,
    69  		DefaultText: `~/.config/authdbctl/token.jwt`,
    70  		EnvVars:     []string{"AUTHDBCTL_TOKEN_PATH"},
    71  	})
    72  	sh.Flags = append(sh.Flags, &cli.StringFlag{
    73  		Name:        "format",
    74  		Usage:       "Sets `NAME` of the output format",
    75  		Value:       `json`,
    76  		DefaultText: `json`,
    77  		EnvVars:     []string{"AUTHDBCTL_OUTPUT_FORMAT"},
    78  	})
    79  	sh.Flags = append(sh.Flags, &cli.BoolFlag{
    80  		Name:  "debug",
    81  		Usage: "Enabled debug logging",
    82  	})
    83  	sh.Commands = []*cli.Command{
    84  		{
    85  			Name:   "connect",
    86  			Usage:  "connect to auth portal and obtain access token",
    87  			Action: connect,
    88  		},
    89  		{
    90  			Name:   "metadata",
    91  			Usage:  "fetch metadata",
    92  			Action: metadata,
    93  		},
    94  		{
    95  			Name:        "add",
    96  			Usage:       "add database objects",
    97  			Subcommands: addSubcmd,
    98  		},
    99  		{
   100  			Name:        "list",
   101  			Usage:       "list database objects",
   102  			Subcommands: listSubcmd,
   103  		},
   104  	}
   105  }
   106  
   107  func main() {
   108  	err := sh.Run(os.Args)
   109  	if err != nil {
   110  		log.Fatal(err)
   111  	}
   112  }