未验证 提交 6d696925 编写于 作者: JenkinsInChina's avatar JenkinsInChina 提交者: GitHub

Improve the self upgrade when no privileges (#437)

* Improve the self upgrade when no privileges

* Add privilege flag to void loop case
上级 68cd99df
......@@ -18,6 +18,7 @@ type PrintOption struct {
// SelfUpgradeOption is the option for self upgrade command
type SelfUpgradeOption struct {
ShowProgress bool
Privilege bool
GitHubClient *github.Client
RoundTripper http.RoundTripper
......
......@@ -18,6 +18,7 @@ import (
"os/exec"
"path/filepath"
"runtime"
"syscall"
)
// NewSelfUpgradeCmd create a command for self upgrade
......@@ -42,6 +43,8 @@ You can upgrade to the latest developing version, please use it like: jcli versi
func (o *SelfUpgradeOption) addFlags(flags *pflag.FlagSet) {
flags.BoolVarP(&o.ShowProgress, "show-progress", "", true,
i18n.T("If you want to show the progress of download Jenkins CLI"))
flags.BoolVarP(&o.Privilege, "privilege", "", true,
i18n.T("Try to take the privilege from system if there's no write permission on jcli"))
}
// RunE is the main point of current command
......@@ -59,6 +62,18 @@ func (o *SelfUpgradeOption) RunE(cmd *cobra.Command, args []string) (err error)
}
var targetF *os.File
if targetF, err = os.OpenFile(targetPath, os.O_CREATE|os.O_RDWR, 0644); err != nil {
if !o.Privilege {
return
}
var sudo string
if sudo, err = exec.LookPath("sudo"); err == nil {
sudoArgs := []string{"sudo", "jcli", "version", "upgrade", "--privilege=false"}
sudoArgs = append(sudoArgs, args...)
env := os.Environ()
err = syscall.Exec(sudo, sudoArgs, env)
}
return
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册