github.com/rootless-containers/rootlesskit/v2@v2.3.4/cmd/rootlessctl/main.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/sirupsen/logrus"
    10  	"github.com/urfave/cli/v2"
    11  
    12  	"github.com/rootless-containers/rootlesskit/v2/pkg/api/client"
    13  	"github.com/rootless-containers/rootlesskit/v2/pkg/version"
    14  )
    15  
    16  func main() {
    17  	debug := false
    18  	app := cli.NewApp()
    19  	app.Name = "rootlessctl"
    20  	app.Version = version.Version
    21  	app.Usage = "RootlessKit API client"
    22  	app.Flags = []cli.Flag{
    23  		&cli.BoolFlag{
    24  			Name:        "debug",
    25  			Usage:       "debug mode",
    26  			Destination: &debug,
    27  		},
    28  		&cli.StringFlag{
    29  			Name:  "socket",
    30  			Usage: "Path to api.sock (under the \"rootlesskit --state-dir\" directory), defaults to $ROOTLESSKIT_STATE_DIR/api.sock",
    31  		},
    32  	}
    33  	app.Commands = []*cli.Command{
    34  		&listPortsCommand,
    35  		&addPortsCommand,
    36  		&removePortsCommand,
    37  		&infoCommand,
    38  	}
    39  	app.Before = func(clicontext *cli.Context) error {
    40  		if debug {
    41  			logrus.SetLevel(logrus.DebugLevel)
    42  		}
    43  		return nil
    44  	}
    45  	if err := app.Run(os.Args); err != nil {
    46  		if debug {
    47  			fmt.Fprintf(os.Stderr, "error: %+v\n", err)
    48  		} else {
    49  			fmt.Fprintf(os.Stderr, "error: %v\n", err)
    50  		}
    51  		os.Exit(1)
    52  	}
    53  }
    54  
    55  func newClient(clicontext *cli.Context) (client.Client, error) {
    56  	socketPath := clicontext.String("socket")
    57  	if socketPath == "" {
    58  		stateDir := os.Getenv("ROOTLESSKIT_STATE_DIR")
    59  		if stateDir == "" {
    60  			return nil, errors.New("please specify --socket or set $ROOTLESSKIT_STATE_DIR")
    61  		}
    62  		socketPath = filepath.Join(stateDir, "api.sock")
    63  	}
    64  	return client.New(socketPath)
    65  }