Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ linters:
# - rowserrcheck
# style
- asciicheck
- depguard
# depguard
- dogsled
- dupl
# - exhaustivestruct
Expand All @@ -62,7 +62,6 @@ linters:
- gomodguard
- goprintffuncname
- gosimple
- ifshort
- importas
# - interfacer
- lll
Expand All @@ -82,13 +81,10 @@ linters:
- whitespace
- wrapcheck
- wsl
# unused
- deadcode
- unused
- ineffassign
- structcheck
- unparam
- unused
- varcheck
fast: false

linters-settings:
Expand Down
2 changes: 1 addition & 1 deletion cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ value takes precedence over the --persona flag.
ui.Infof("Encryption passphrase is: %s", passphrase)
},
}

backupCmd.Flags().String("persona", "", "The persona to backup")

return backupCmd

}
5 changes: 3 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cmd

import (
"github.com/endorama/devid/cmd/ui"
"github.com/spf13/cobra"

"github.com/endorama/devid/cmd/ui"
)

var cli *cobra.Command
var cli *cobra.Command //nolint:gochecknoglobals // one cli to rule them all

// Init initialises a cobra CLI with all commands from this package.
func Init() {
Expand Down
190 changes: 95 additions & 95 deletions cmd/rehash.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/endorama/devid/cmd/ui"
cmdutils "github.com/endorama/devid/cmd/utils"
"github.com/endorama/devid/internal/persona"
"github.com/endorama/devid/internal/plugin/manager"
"github.com/endorama/devid/internal/utils"
)
Expand All @@ -50,103 +51,102 @@ goes bad you can still load a persona's shell environment by running

rehash command is directly inspired by rbenv, a ruby version manager.
`,
Run: func(cmd *cobra.Command, args []string) {
if viper.GetString("active_persona") != "" {
// NOTE: rehashing when a profile is active is dangerous, as the environment
// has been changed with customizations and there is no guarantee about
// what those changes have affected.
// This may be especially problematic for executable path detection in
// plugin.
// As such we prevent rehashing while there is an active profile.
ui.Fatal(errRehashWithActiveProfile, genericExitCode)
}

var errs []error
var err error

p, err := cmdutils.LoadPersona(cmd)
if err != nil {
ui.Fatal(fmt.Errorf("cannot instantiate persona: %w", err), noPersonaLoadedExitCode)
}
// errs, err := manager.LoadPlugins(p.Config)
// if err != nil {
// ui.Error(err.Error())
//
// for _, e := range errs {
// ui.Error(e.Error())
// }
//
// os.Exit(pluginManagerLoadingErrorExitCode)
// }

errs, err = manager.LoadCorePlugins(p.Config)
if err != nil {
ui.Error(err)

for _, e := range errs {
ui.Error(e)
}

os.Exit(pluginManagerCoreLoadingErrorExitCode)
}

errs, err = manager.LoadOptionalPlugins(p.Config)
if err != nil {
ui.Error(err)

for _, e := range errs {
ui.Error(e)
}

os.Exit(pluginManagerOptionalLoadingErrorExitCode)
}

log.Printf("persona: %+v\n", p)

errs, err = manager.SetupPlugins(p)
if err != nil {
ui.Error(err)

for _, e := range errs {
ui.Error(e)
}

os.Exit(pluginGenerationExitCode)
}

errs, err = manager.Generate(p)
if err != nil {
ui.Error(err)

for _, e := range errs {
ui.Error(e)
}

os.Exit(pluginGenerationExitCode)
}

content, err := manager.ShellLoader(p)
if err != nil {
ui.Error(err)
os.Exit(1)
}

log.Printf("%+v\n", content)

shellLoaderFilePath := path.Join(p.Location(), viper.GetString("shell_loader_filename"))
err = utils.PersistFile(shellLoaderFilePath, content)
if err != nil {
ui.Fatal(fmt.Errorf("cannot save shell loader: %w", err), genericExitCode)
}

if err := os.Chmod(shellLoaderFilePath, permUserRWX); err != nil {
ui.Fatal(
fmt.Errorf("cannot change permissions to %s on %s: %v", permUserRWX, shellLoaderFilePath, err),
genericExitCode)
}
},
Run: runRehash,
}

rehashCmd.Flags().String("persona", "", "The persona for which to rebuild the shell configuration")

return rehashCmd
}

func runRehash(cmd *cobra.Command, _ []string) {
if viper.GetString("active_persona") != "" {
// NOTE: rehashing when a profile is active is dangerous, as the environment
// has been changed with customizations and there is no guarantee about
// what those changes have affected.
// This may be especially problematic for executable path detection in
// plugin.
// As such we prevent rehashing while there is an active profile.
ui.Fatal(errRehashWithActiveProfile, genericExitCode)
}

var (
errs []error
err error
)

p, err := cmdutils.LoadPersona(cmd)
if err != nil {
ui.Fatal(fmt.Errorf("cannot instantiate persona: %w", err), noPersonaLoadedExitCode)
}
// errs, err := manager.LoadPlugins(p.Config)
// if err != nil {
// ui.Error(err.Error())
//
// for _, e := range errs {
// ui.Error(e.Error())
// }
//
// os.Exit(pluginManagerLoadingErrorExitCode)
// }

errs, err = manager.LoadCorePlugins(p.Config)
if err != nil {
handleAllErrors(errs, err, pluginManagerCoreLoadingErrorExitCode)
}

errs, err = manager.LoadOptionalPlugins(p.Config)
if err != nil {
handleAllErrors(errs, err, pluginManagerOptionalLoadingErrorExitCode)
}

log.Printf("persona: %+v\n", p)

errs, err = manager.SetupPlugins(p)
if err != nil {
handleAllErrors(errs, err, pluginGenerationExitCode)
}

errs, err = manager.Generate(p)
if err != nil {
handleAllErrors(errs, err, pluginGenerationExitCode)
}

fn := viper.GetString("shell_loader_filename")
if err := writeShellLoader(p, fn); err != nil {
ui.Fatal(err, genericExitCode)
}
}

func handleAllErrors(errs []error, err error, exitCode int) {
ui.Error(err)

for _, e := range errs {
ui.Error(e)
}

os.Exit(exitCode)
}

func writeShellLoader(p persona.Persona, filename string) error {
content, err := manager.ShellLoader(p)
if err != nil {
ui.Error(err)
os.Exit(1)
}

log.Printf("%+v\n", content)

shellLoaderFilePath := path.Join(p.Location(), filename)

err = utils.PersistFile(shellLoaderFilePath, content)
if err != nil {
return fmt.Errorf("cannot save shell loader: %w", err)
}

if err := os.Chmod(shellLoaderFilePath, permUserRWX); err != nil {
return fmt.Errorf("cannot change permissions to %s on %s: %w", permUserRWX, shellLoaderFilePath, err)
}

return nil
}
2 changes: 0 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ var cfgFile string //nolint:gochecknoglobals // required for init
var verbose bool //nolint:gochecknoglobals // require for init

func RootCmd() *cobra.Command {
// rootCmd represents the base command when called without any subcommands.
var rootCmd = &cobra.Command{ //nolint:gochecknoglobals // required by cobra
Use: "devid",
Short: "Secure manager for your developer personas",
Expand Down Expand Up @@ -68,7 +67,6 @@ for persona's folders.

func init() { //nolint:gochecknoinits // required by cobra
cobra.OnInitialize(initConfig)

}

// initConfig reads in config file and ENV variables if set.
Expand Down
1 change: 1 addition & 0 deletions cmd/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ value takes precedence over the --persona flag.`,
}

shellCmd.Flags().String("persona", "", "The persona's shell to load")

return shellCmd
}
1 change: 1 addition & 0 deletions cmd/whoami.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,6 @@ value takes precedence over the --persona flag.
}

whoamiCmd.Flags().BoolP("extended", "e", false, "Print extended identity information")

return whoamiCmd
}
22 changes: 0 additions & 22 deletions internal/persona/new.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package persona

import (
"errors"
"fmt"
"path"

"github.com/spf13/viper"
Expand Down Expand Up @@ -35,23 +33,3 @@ func NewWithCustomLocation(name, location string) (Persona, error) {
Config: v,
}, nil
}

var errPersonaDoesNotExists = errors.New("does not exists")

func Load(name string) (Persona, error) {
p, err := New(name)
if err != nil {
return p, fmt.Errorf("init failed: %w", err)
}

if !p.Exists() {
return p, fmt.Errorf("%w in %s", errPersonaDoesNotExists, p.Location())
}

err = p.Load()
if err != nil {
return p, fmt.Errorf("cannot load persona configuration: %w", err)
}

return p, nil
}
3 changes: 2 additions & 1 deletion internal/plugin/manager/shellfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
"github.com/endorama/devid/internal/plugin"
)

//
//nolint:gochecknoglobals // required by embed
//go:embed load.sh.txt
// nolint:gochecknoglobals // required by embed
var shellLoader string

// ShellLoader generate profile shell loader file.
Expand Down
2 changes: 1 addition & 1 deletion internal/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func setDefaults() {
viper.SetDefault("shell", os.Getenv("SHELL"))
}

// nolint:deadcode,unused // will use it
//nolint:deadcode,unused // will use it
func readConfigFile() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
Expand Down
2 changes: 1 addition & 1 deletion internal/utils/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func OpenWithEditor(path string) error {
// AllowedEditors is a list of allowed values for the EDITOR environment variable.
// FIXME: prevent unknown command execution when some of these editor is not available.
// FIXME: make this a function so is not directly modifiable.
var AllowedEditors = []string{ // nolint:gochecknoglobals // implementation detail
var AllowedEditors = []string{ //nolint:gochecknoglobals // implementation detail
"/bin/ed",
"/bin/nano",
"/usr/bin/vim",
Expand Down
2 changes: 1 addition & 1 deletion plugins/bin/renderable.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package bin

import "strings"

func (p Plugin) Render(profileName, profileLocation string) string {
func (p Plugin) Render(_, profileLocation string) string {
sb := strings.Builder{}
sb.WriteString("export PATH=\"" + profileLocation + "/bin:$PATH\"\n")

Expand Down
2 changes: 1 addition & 1 deletion plugins/envs/renderable.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
)

func (p Plugin) Render(profileName, profileLocation string) string {
func (p Plugin) Render(_, _ string) string {
sb := strings.Builder{}

for name, value := range p.config {
Expand Down
4 changes: 2 additions & 2 deletions plugins/gcp/plugin.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gcp

// TODO: add gcp.NewPlugin() to internal/plugin/manager/available.go under Optional
// TODO: add enabled check in internal/plugin/manager/manager.go LoadOptionalPlugins switch case
// TODO: add gcp.NewPlugin() to internal/plugin/manager/available.go under Optional.
// TODO: add enabled check in internal/plugin/manager/manager.go LoadOptionalPlugins switch case.
const pluginName = "gcp"

type Plugin struct {
Expand Down
2 changes: 1 addition & 1 deletion plugins/gcp/renderable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "strings"

// Render returns content rendered by the plugin.
// Implements `plugin.Renderable` interface.
func (p Plugin) Render(personaName, personaDirectory string) string {
func (p Plugin) Render(_, _ string) string {
// TODO: implement rendering logic
sb := strings.Builder{}
// sb.WriteString("plugin rendered content")
Expand Down
Loading