Skip to content

Commit

Permalink
Merge pull request #1385 from montag451/aliases-comp
Browse files Browse the repository at this point in the history
incus: Add aliases completion
  • Loading branch information
stgraber authored Nov 25, 2024
2 parents 0e653cc + bb184cb commit d1c5c1b
Show file tree
Hide file tree
Showing 16 changed files with 440 additions and 363 deletions.
63 changes: 30 additions & 33 deletions cmd/incus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"os/user"
"path"
"path/filepath"
"slices"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -71,6 +70,27 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.{{e
`
}

func aliases() []string {
c, err := config.LoadConfig("")
if err != nil {
return nil
}

aliases := make([]string, 0, len(defaultAliases)+len(c.Aliases))

// Add default aliases
for alias := range defaultAliases {
aliases = append(aliases, alias)
}

// Add user-defined aliases
for alias := range c.Aliases {
aliases = append(aliases, alias)
}

return aliases
}

func main() {
// Process aliases
err := execIfAliases()
Expand All @@ -93,6 +113,7 @@ Custom commands can be defined through aliases, use "incus alias" to control tho
app.SilenceUsage = true
app.SilenceErrors = true
app.CompletionOptions = cobra.CompletionOptions{HiddenDefaultCmd: true}
app.ValidArgs = aliases()

// Global flags
globalCmd := cmdGlobal{cmd: app, asker: ask.NewAsker(bufio.NewReader(os.Stdin))}
Expand Down Expand Up @@ -337,23 +358,6 @@ func (c *cmdGlobal) PreRun(cmd *cobra.Command, args []string) error {
return nil
}

// Figure out the config directory and config path
var configDir string
if os.Getenv("INCUS_CONF") != "" {
configDir = os.Getenv("INCUS_CONF")
} else if os.Getenv("HOME") != "" && util.PathExists(os.Getenv("HOME")) {
configDir = path.Join(os.Getenv("HOME"), ".config", "incus")
} else {
user, err := user.Current()
if err != nil {
return err
}

if util.PathExists(user.HomeDir) {
configDir = path.Join(user.HomeDir, ".config", "incus")
}
}

// Figure out a potential cache path.
var cachePath string
if os.Getenv("INCUS_CACHE") != "" {
Expand All @@ -378,25 +382,18 @@ func (c *cmdGlobal) PreRun(cmd *cobra.Command, args []string) error {
}
}

// If no homedir could be found, treat as if --force-local was passed.
if configDir == "" {
c.flagForceLocal = true
c.conf, err = config.LoadConfig("")
if err != nil {
return fmt.Errorf(i18n.G("Failed to load configuration: %s"), err)
}

c.confPath = os.ExpandEnv(path.Join(configDir, "config.yml"))

// Load the configuration
if c.flagForceLocal {
c.conf = config.NewConfig("", true)
} else if util.PathExists(c.confPath) {
c.conf, err = config.LoadConfig(c.confPath)
if err != nil {
return err
}
} else {
c.conf = config.NewConfig(filepath.Dir(c.confPath), true)
// If no config dir could be found, treat as if --force-local was passed.
if c.conf.ConfigDir == "" {
c.flagForceLocal = true
}

c.confPath = c.conf.ConfigPath("config.yml")

// Set cache directory in config.
c.conf.CacheDir = cachePath

Expand Down
34 changes: 3 additions & 31 deletions cmd/incus/main_aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import (
"fmt"
"os"
"os/exec"
"os/user"
"path"
"path/filepath"
"regexp"
"slices"
"strconv"
Expand All @@ -16,7 +13,6 @@ import (

"github.com/lxc/incus/v6/internal/i18n"
config "github.com/lxc/incus/v6/shared/cliconfig"
"github.com/lxc/incus/v6/shared/util"
)

var numberedArgRegex = regexp.MustCompile(`@ARG(\d+)@`)
Expand Down Expand Up @@ -212,33 +208,9 @@ func execIfAliases() error {
return nil
}

// Figure out the config directory and config path
var configDir string
if os.Getenv("INCUS_CONF") != "" {
configDir = os.Getenv("INCUS_CONF")
} else if os.Getenv("HOME") != "" {
configDir = path.Join(os.Getenv("HOME"), ".config", "incus")
} else {
user, err := user.Current()
if err != nil {
return nil
}

configDir = path.Join(user.HomeDir, ".config", "incus")
}

confPath := os.ExpandEnv(path.Join(configDir, "config.yml"))

// Load the configuration
var conf *config.Config
var err error
if util.PathExists(confPath) {
conf, err = config.LoadConfig(confPath)
if err != nil {
return nil
}
} else {
conf = config.NewConfig(filepath.Dir(confPath), true)
conf, err := config.LoadConfig("")
if err != nil {
return fmt.Errorf(i18n.G("Failed to load configuration: %s"), err)
}

// Expand the aliases
Expand Down
51 changes: 28 additions & 23 deletions po/de.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: LXD\n"
"Report-Msgid-Bugs-To: [email protected]\n"
"POT-Creation-Date: 2024-11-14 01:18-0500\n"
"POT-Creation-Date: 2024-11-16 15:36+0100\n"
"PO-Revision-Date: 2024-10-06 20:15+0000\n"
"Last-Translator: Jan Mittelstädter <[email protected]>\n"
"Language-Team: German <https://hosted.weblate.org/projects/incus/cli/de/>\n"
Expand Down Expand Up @@ -1598,11 +1598,11 @@ msgstr "Clustering aktiviert"
msgid "Columns"
msgstr "Spalten"

#: cmd/incus/main.go:85
#: cmd/incus/main.go:101
msgid "Command line client for Incus"
msgstr "Kommandozeilen-Client für Incus"

#: cmd/incus/main.go:86
#: cmd/incus/main.go:102
msgid ""
"Command line client for Incus\n"
"\n"
Expand Down Expand Up @@ -2228,7 +2228,7 @@ msgstr ""
#: cmd/incus/image_alias.go:29 cmd/incus/image_alias.go:65
#: cmd/incus/image_alias.go:112 cmd/incus/image_alias.go:158
#: cmd/incus/image_alias.go:334 cmd/incus/import.go:27 cmd/incus/info.go:35
#: cmd/incus/launch.go:24 cmd/incus/list.go:50 cmd/incus/main.go:86
#: cmd/incus/launch.go:24 cmd/incus/list.go:50 cmd/incus/main.go:102
#: cmd/incus/manpage.go:22 cmd/incus/monitor.go:33 cmd/incus/move.go:35
#: cmd/incus/network.go:36 cmd/incus/network.go:143 cmd/incus/network.go:240
#: cmd/incus/network.go:337 cmd/incus/network.go:448 cmd/incus/network.go:506
Expand Down Expand Up @@ -2549,7 +2549,7 @@ msgstr ""
msgid "Don't require user confirmation for using --force"
msgstr ""

#: cmd/incus/main.go:106
#: cmd/incus/main.go:123
msgid "Don't show progress information"
msgstr ""

Expand Down Expand Up @@ -3291,6 +3291,11 @@ msgstr "kann nicht zum selben Container Namen kopieren"
msgid "Failed to listen for connection: %w"
msgstr "Akzeptiere Zertifikat"

#: cmd/incus/main.go:383 cmd/incus/main_aliases.go:213
#, fuzzy, c-format
msgid "Failed to load configuration: %s"
msgstr ""

#: cmd/incus/admin_sql.go:114
#, fuzzy, c-format
msgid "Failed to parse dump response: %w"
Expand Down Expand Up @@ -3463,7 +3468,7 @@ msgstr "Herunterfahren des Containers erzwingen."
msgid "Force the removal of running instances"
msgstr ""

#: cmd/incus/main.go:102
#: cmd/incus/main.go:119
msgid "Force using the local unix socket"
msgstr ""

Expand Down Expand Up @@ -3525,7 +3530,7 @@ msgstr ""
msgid "Forward delay"
msgstr ""

#: cmd/incus/main_aliases.go:140
#: cmd/incus/main_aliases.go:136
#, c-format
msgid "Found alias %q references an argument outside the given number"
msgstr ""
Expand Down Expand Up @@ -3875,7 +3880,7 @@ msgstr ""
msgid "If the snapshot name already exists, delete and create a new one"
msgstr ""

#: cmd/incus/main.go:451
#: cmd/incus/main.go:444
msgid ""
"If this is your first time running Incus on this machine, you should also "
"run: incus admin init"
Expand Down Expand Up @@ -4081,7 +4086,7 @@ msgstr "Ungültiges Ziel %s"
msgid "Invalid URL scheme \"%s\" in \"%s\""
msgstr ""

#: cmd/incus/main_aliases.go:136 cmd/incus/main_aliases.go:179
#: cmd/incus/main_aliases.go:132 cmd/incus/main_aliases.go:175
#, fuzzy, c-format
msgid "Invalid argument %q"
msgstr "Ungültiges Ziel %s"
Expand Down Expand Up @@ -4182,7 +4187,7 @@ msgid ""
"Invalid name in '%s', empty string is only allowed when defining maxWidth"
msgstr ""

#: cmd/incus/main.go:547 cmd/incus/storage.go:134
#: cmd/incus/main.go:540 cmd/incus/storage.go:134
#, fuzzy
msgid "Invalid number of arguments"
msgstr "ungültiges Argument %s"
Expand Down Expand Up @@ -5480,8 +5485,8 @@ msgstr "Kein Zertifikat für diese Verbindung"
msgid ""
"Manage storage volumes\n"
"\n"
"Unless specified through a prefix, all volume operations affect "
"\"custom\" (user created) volumes."
"Unless specified through a prefix, all volume operations affect \"custom\" "
"(user created) volumes."
msgstr ""

#: cmd/incus/remote.go:43 cmd/incus/remote.go:44
Expand Down Expand Up @@ -6298,7 +6303,7 @@ msgstr "Profil %s gelöscht\n"
msgid "Optimized Storage"
msgstr ""

#: cmd/incus/main.go:103
#: cmd/incus/main.go:120
msgid "Override the source project"
msgstr ""

Expand Down Expand Up @@ -6378,7 +6383,7 @@ msgstr ""
msgid "Partitions:"
msgstr ""

#: cmd/incus/main.go:412
#: cmd/incus/main.go:405
#, fuzzy, c-format
msgid "Password for %s: "
msgstr "Administrator Passwort für %s: "
Expand Down Expand Up @@ -6484,15 +6489,15 @@ msgstr ""
msgid "Pretty rendering (short for --format=pretty)"
msgstr ""

#: cmd/incus/main.go:101
#: cmd/incus/main.go:118
msgid "Print help"
msgstr ""

#: cmd/incus/query.go:43
msgid "Print the raw response"
msgstr ""

#: cmd/incus/main.go:100
#: cmd/incus/main.go:117
msgid "Print version number"
msgstr ""

Expand All @@ -6501,7 +6506,7 @@ msgstr ""
msgid "Processes: %d"
msgstr "Profil %s erstellt\n"

#: cmd/incus/main_aliases.go:255 cmd/incus/main_aliases.go:262
#: cmd/incus/main_aliases.go:227 cmd/incus/main_aliases.go:234
#, fuzzy, c-format
msgid "Processing aliases failed: %s"
msgstr "Anhalten des Containers fehlgeschlagen!"
Expand Down Expand Up @@ -7568,11 +7573,11 @@ msgstr ""
msgid "Setup loop based storage with SIZE in GiB"
msgstr ""

#: cmd/incus/main.go:104
#: cmd/incus/main.go:121
msgid "Show all debug messages"
msgstr ""

#: cmd/incus/main.go:105
#: cmd/incus/main.go:122
msgid "Show all information messages"
msgstr ""

Expand Down Expand Up @@ -7627,7 +7632,7 @@ msgstr "Profil %s erstellt\n"
msgid "Show instance snapshot configuration"
msgstr "Profil %s erstellt\n"

#: cmd/incus/main.go:284 cmd/incus/main.go:285
#: cmd/incus/main.go:301 cmd/incus/main.go:302
msgid "Show less common commands"
msgstr ""

Expand Down Expand Up @@ -8357,7 +8362,7 @@ msgstr ""
msgid "There is no \"image name\". Did you want an alias?"
msgstr ""

#: cmd/incus/main.go:311
#: cmd/incus/main.go:328
msgid ""
"This client hasn't been configured to use a remote server yet.\n"
"As your platform can't run native Linux instances, you must connect to a "
Expand Down Expand Up @@ -8410,7 +8415,7 @@ msgstr ""
msgid "To detach from the console, press: <ctrl>+a q"
msgstr ""

#: cmd/incus/main.go:456
#: cmd/incus/main.go:449
msgid ""
"To start your first container, try: incus launch images:ubuntu/22.04\n"
"Or for a virtual machine: incus launch images:ubuntu/22.04 --vm"
Expand Down Expand Up @@ -8857,7 +8862,7 @@ msgid ""
"Use storage driver optimized format (can only be restored on a similar pool)"
msgstr ""

#: cmd/incus/main.go:107
#: cmd/incus/main.go:124
msgid "Use with help or --help to view sub-commands"
msgstr ""

Expand Down
Loading

0 comments on commit d1c5c1b

Please sign in to comment.