From cb09cecbf7d0e86695104bfcde77919edf7150ab Mon Sep 17 00:00:00 2001 From: Tamer Tas <contact@tmrts.com> Date: Thu, 11 Feb 2016 06:21:03 +0200 Subject: [PATCH] Update shell completion configuration to remove root privileges prompt --- install | 18 ++++++++++-------- pkg/boilr/configuration.go | 2 ++ pkg/cmd/bash_completion.go | 34 +++++++++++++++++++++++++--------- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/install b/install index d2f1108..52d8045 100755 --- a/install +++ b/install @@ -31,20 +31,20 @@ symlink() { } configure() { - # Auto-completion + $HOME/bin/boilr init + if [ $? -ne 0 ]; then + binary_error="Failed to complete boilr initialization" + return + fi + + # Auto-completion prompt if [ -z "$auto_completion" ]; then ask "Do you want to enable auto-completion for boilr commmands?" auto_completion=$? fi if [ $auto_completion -eq 1 ]; then - sudo $HOME/bin/boilr configure-bash-completion - fi - - $HOME/bin/boilr init - if [ $? -ne 0 ]; then - binary_error="Failed to complete boilr initialization" - return + $HOME/bin/boilr configure-bash-completion fi } @@ -124,5 +124,7 @@ esac cat << EOF Completed installation +Boilr executable is installed to ~/bin/boilr + For more information, see: https://github.com/tmrts/boilr EOF diff --git a/pkg/boilr/configuration.go b/pkg/boilr/configuration.go index 37b2775..392fe78 100644 --- a/pkg/boilr/configuration.go +++ b/pkg/boilr/configuration.go @@ -48,6 +48,7 @@ const ( // file in the configuration directory. var Configuration = struct { FilePath string + ConfigDirPath string TemplateDirPath string }{} @@ -68,6 +69,7 @@ func init() { } Configuration.FilePath = filepath.Join(homeDir, ConfigDirPath, ConfigFileName) + Configuration.ConfigDirPath = filepath.Join(homeDir, ConfigDirPath) Configuration.TemplateDirPath = filepath.Join(homeDir, ConfigDirPath, TemplateDir) // Read .config/boilr/config.json if exists diff --git a/pkg/cmd/bash_completion.go b/pkg/cmd/bash_completion.go index e0cd983..7c7c720 100644 --- a/pkg/cmd/bash_completion.go +++ b/pkg/cmd/bash_completion.go @@ -1,29 +1,45 @@ package cmd import ( + "errors" "fmt" "os" "path/filepath" - "strings" cli "github.com/spf13/cobra" "github.com/tmrts/boilr/pkg/boilr" "github.com/tmrts/boilr/pkg/util/exit" ) +const bashrcText = ` +# Enables shell command completion for boilr +source $HOME/bin/boilr +` + func configureBashCompletion() error { - var bash_completion_dir string - if bash_completion_dir = os.Getenv("BASH_COMPLETION_COMPAT_DIR"); bash_completion_dir == "" { - bash_completion_dir = "/etc/bash_completion.d" - } + bash_completion_file := filepath.Join(boilr.Configuration.ConfigDirPath, "completion.bash") - bash_completion_file := filepath.Join(bash_completion_dir, boilr.AppName+".bash") + if err := Root.GenBashCompletionFile(bash_completion_file); err != nil { + return err + } if err := Root.GenBashCompletionFile(bash_completion_file); err != nil { - if strings.Contains(err.Error(), "permission") { - return fmt.Errorf("couldn't configure bash completion for %s: permission denied", boilr.AppName) - } + return err + } + + bashrcPath := filepath.Join(os.Getenv("HOME"), ".bashrc") + if bashrcPath == "" { + return errors.New("environment variable ${HOME} should be set") + } + + f, err := os.OpenFile(bashrcPath, os.O_APPEND|os.O_WRONLY, 0600) + if err != nil { + return err + } + + defer f.Close() + if _, err = f.WriteString(bashrcText); err != nil { return err } -- GitLab