File: main.go

package info (click to toggle)
rootlesskit 2.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 636 kB
  • sloc: sh: 433; makefile: 25
file content (65 lines) | stat: -rw-r--r-- 1,491 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main

import (
	"errors"
	"fmt"
	"os"
	"path/filepath"

	"github.com/sirupsen/logrus"
	"github.com/urfave/cli/v2"

	"github.com/rootless-containers/rootlesskit/v2/pkg/api/client"
	"github.com/rootless-containers/rootlesskit/v2/pkg/version"
)

func main() {
	debug := false
	app := cli.NewApp()
	app.Name = "rootlessctl"
	app.Version = version.Version
	app.Usage = "RootlessKit API client"
	app.Flags = []cli.Flag{
		&cli.BoolFlag{
			Name:        "debug",
			Usage:       "debug mode",
			Destination: &debug,
		},
		&cli.StringFlag{
			Name:  "socket",
			Usage: "Path to api.sock (under the \"rootlesskit --state-dir\" directory), defaults to $ROOTLESSKIT_STATE_DIR/api.sock",
		},
	}
	app.Commands = []*cli.Command{
		&listPortsCommand,
		&addPortsCommand,
		&removePortsCommand,
		&infoCommand,
	}
	app.Before = func(clicontext *cli.Context) error {
		if debug {
			logrus.SetLevel(logrus.DebugLevel)
		}
		return nil
	}
	if err := app.Run(os.Args); err != nil {
		if debug {
			fmt.Fprintf(os.Stderr, "error: %+v\n", err)
		} else {
			fmt.Fprintf(os.Stderr, "error: %v\n", err)
		}
		os.Exit(1)
	}
}

func newClient(clicontext *cli.Context) (client.Client, error) {
	socketPath := clicontext.String("socket")
	if socketPath == "" {
		stateDir := os.Getenv("ROOTLESSKIT_STATE_DIR")
		if stateDir == "" {
			return nil, errors.New("please specify --socket or set $ROOTLESSKIT_STATE_DIR")
		}
		socketPath = filepath.Join(stateDir, "api.sock")
	}
	return client.New(socketPath)
}