cli

package
v0.0.0-...-1b913b8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 11, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package cli provides the command line interface for the bucket application.

Index

Constants

View Source
const (
	UP = iota + 1
	DOWN
	RIGHT
	LEFT
)
View Source
const CTRLC = '\x03'
View Source
const ENTER = -13
View Source
const SEQUENCE = '\x1b'

Variables

View Source
var ADD = &cli.Command{
	Name:    "add",
	Aliases: []string{"a"},
	Usage:   "adds a plugin to the server",
	Before:  InitializeContexts(true),
	After:   ShutdownContexts,

	Args:      true,
	ArgsUsage: " name",

	Action: func(c *cli.Context) error {
		if c.Args().Len() == 0 {
			return cli.Exit("missing plugin name", 1)
		}

		return Workspace.RunWithContext("add", func(oc *bucket.OpenContext, log *log.Logger) error {
			const REPO = "modrinth"

			if oc.Platform == nil {
				return cli.Exit("no platform set", 1)
			}

			res, _, err := oc.Repositories[REPO].Search(c.Args().Get(0), 5)
			if err != nil {
				return err
			}

			if len(res) > 5 {
				res = res[:5]
			} else if len(res) == 0 {
				return cli.Exit("no plugins found", 1)
			}

			options := make([]string, len(res))
			for i, v := range res {
				options[i] = fmt.Sprintf("[%s] %s", v.GetRepository().Provider(), v.GetName())
			}

			log.Println("Select a plugin to install")

			n, err := TableSelect(options, os.Stderr)

			if err != nil {
				return err
			}

			log.Printf("Selected %s\n\n", options[n])

			pl := res[n]
			ver, err := pl.GetLatestVersion()
			if err != nil {
				return err
			}

			log.Printf("Installing %s [%s]\n", pl.GetName(), ver.GetIdentifier())

			files, err := ver.GetFiles()
			if err != nil {
				return err
			}

			oc.Fs.MkdirAll(oc.Platform.PluginsFolder(), 0755)
			folder := afero.NewBasePathFs(oc.Fs, oc.Platform.PluginsFolder())

			for _, f := range files {
				if !f.Optional() {
					log.Printf("Downloading %s\n", f.Name())

					data, err := f.Download()
					if err != nil {
						return err
					}

					defer data.Close()

					fd, err := folder.OpenFile(f.Name(), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
					if err != nil {
						return err
					}

					defer fd.Close()

					if _, err := io.Copy(fd, data); err != nil {
						return err
					}

					if err := fd.Close(); err != nil {
						return err
					}

					if err := f.Verify(); err != nil {
						return err
					} else {
						log.Printf("File %s verified\n", f.Name())
					}

					pl, err := oc.Platform.LoadPlugin(f.Name())
					if err != nil {
						return err
					}

					err = oc.SavePlugin(bucket.CachedMatch(pl, ver, oc.Repositories[REPO], 1.0))
					if err != nil {
						return err
					}

					log.Printf("Plugin %s saved\n", pl.GetName())
				} else {
					log.Printf("Skipping optional file %s\n", f.Name())
				}
			}

			return nil
		})
	},
}
View Source
var CLEAN = &cli.Command{
	Name:    "clean",
	Aliases: []string{"c"},
	Usage:   "discards the plugin cache",
	Before:  InitializeContexts(false),
	After:   ShutdownContexts,
	Action: func(c *cli.Context) error {
		return Workspace.RunWithContext("clean", func(oc *bucket.OpenContext, log *log.Logger) error {
			size, err := oc.DBSize()
			if err != nil {
				return err
			}

			log.Printf("deleting database file (%d KB)\n", size/1024)
			if err := oc.CleanCache(); err != nil {
				return err
			}

			if c.Args().Len() > 0 {
				if c.Args().Get(0) == "all" {
					var size int64

					if size, err = oc.PluginsSize(); err != nil {
						return err
					}

					log.Printf("deleting plugin cache (%.2f MB)\n", float64(size)/1024/1024)
					if err = oc.Fs.RemoveAll(oc.Platform.PluginsFolder()); err != nil {
						return err
					}
				}
			}

			return nil
		})
	},
}
View Source
var Commands = []*cli.Command{
	ADD, CLEAN, DEBUG,
}
View Source
var DEBUG = &cli.Command{
	Name:   "debug",
	Usage:  "runs debug routine",
	Before: InitializeContexts(true),
	After:  ShutdownContexts,
	Action: func(c *cli.Context) error {
		return Workspace.RunWithContext("debug", func(oc *bucket.OpenContext, log *log.Logger) error {
			return bucket.DebugRoutine(oc, log)
		})
	},
}
View Source
var GlobalError error
View Source
var Time time.Time
View Source
var Workspace *bucket.Workspace

Functions

func InitializeContexts

func InitializeContexts(loadDatabase bool) func(*cli.Context) error

func ReadKeycode

func ReadKeycode(in io.Reader) (int, error)

func ReadSequence

func ReadSequence(in io.Reader) (int, error)

func ShutdownContexts

func ShutdownContexts(c *cli.Context) error

func TableSelect

func TableSelect(options []string, out io.Writer) (int, error)

func TableSelectPlain

func TableSelectPlain(options []string, out io.Writer) (int, error)

Types

type Table

type Table struct {
	Options  []string
	Selected int

	Writer io.Writer
}

func (*Table) MoveDown

func (t *Table) MoveDown()

func (*Table) MoveUp

func (t *Table) MoveUp()

func (*Table) Render

func (t *Table) Render(cursor bool, indexes bool)

func (*Table) Rollback

func (t *Table) Rollback()

Directories

Path Synopsis
Package tasks provides task definitions for the bucket CLI application.
Package tasks provides task definitions for the bucket CLI application.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL