diff --git a/install b/install index d2f11087d13ef8ed8822e5ff64eb7d334fc653c1..52d804515bb5b1973d32f43be12a44b29bcff715 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 37b2775ebcfd1ccba46696825be04483aca8ca8b..392fe7878a5fa9dde75cd1ce3dbf6c9548cba583 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 e0cd98333056ca4e34ae1c2b05afb8c6696c9df9..7c7c720d3833a207620786e7a2a917edc7e01a45 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 }