Skip to content

Commit

Permalink
Merge pull request #76 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 0.20.0
  • Loading branch information
andyone authored Feb 7, 2019
2 parents 6b3731f + e80b727 commit 6f0d984
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 35 deletions.
98 changes: 66 additions & 32 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"pkg.re/essentialkaos/ek.v10/knf"
"pkg.re/essentialkaos/ek.v10/log"
"pkg.re/essentialkaos/ek.v10/options"
"pkg.re/essentialkaos/ek.v10/passwd"
"pkg.re/essentialkaos/ek.v10/req"
"pkg.re/essentialkaos/ek.v10/signal"
"pkg.re/essentialkaos/ek.v10/sortutil"
Expand All @@ -52,7 +53,7 @@ import (
// App info
const (
APP = "RBInstall"
VER = "0.19.3"
VER = "0.20.0"
DESC = "Utility for installing prebuilt Ruby versions to rbenv"
)

Expand Down Expand Up @@ -105,19 +106,16 @@ const (
CATEGORY_OTHER = "other"
)

// Name of index file
// INDEX_NAME is name of index file
const INDEX_NAME = "index.json"

// Path to config file
// CONFIG_FILE is path to config file
const CONFIG_FILE = "/etc/rbinstall.knf"

// Name of log with failed actions (gem install)
const FAIL_LOG_NAME = "rbinstall-fail.log"

// Value for column without any versions
// NONE_VERSION is value for column without any versions
const NONE_VERSION = "- none -"

// Default category column size
// DEFAULT_CATEGORY_SIZE is default category column size
const DEFAULT_CATEGORY_SIZE = 28

// Default arch names
Expand Down Expand Up @@ -694,16 +692,23 @@ func installCommand(rubyVersion string) {

if knf.GetS(GEMS_INSTALL) != "" {
for _, gem := range strings.Split(knf.GetS(GEMS_INSTALL), " ") {
gemName, gemVersion := parseGemInfo(gem)

gemInstallTask := &Task{
Desc: fmtc.Sprintf("Installing %s", gem),
Desc: fmtc.Sprintf("Installing %s", gemName),
Handler: installGemTaskHandler,
}

_, err = gemInstallTask.Start(info.Name, gem)
if gemVersion != "" {
gemInstallTask.Desc += fmt.Sprintf(" (%s.x)", gemVersion)
} else {
gemInstallTask.Desc += fmt.Sprintf(" (latest)")
}

_, err = gemInstallTask.Start(info.Name, gemName, gemVersion)

if err != nil {
fmtc.NewLine()
printErrorAndExit(err.Error())
fmtc.Printf(" {r}%v{!}\n", err)
}
}
}
Expand Down Expand Up @@ -912,18 +917,20 @@ func checkBinaryTaskHandler(args ...string) (string, error) {

// installGemTaskHandler run gems installing command
func installGemTaskHandler(args ...string) (string, error) {
version := args[0]
rubyVersion := args[0]
gem := args[1]
gemVersion := args[2]

return runGemCmd(version, "install", gem)
return runGemCmd(rubyVersion, "install", gem, gemVersion)
}

// updateGemTaskHandler run gems update command
func updateGemTaskHandler(args ...string) (string, error) {
version := args[0]
rubyVersion := args[0]
gem := args[1]
gemVersion := args[2]

return runGemCmd(version, "update", gem)
return runGemCmd(rubyVersion, "update", gem, gemVersion)
}

// updateRubygemsTaskHandler run rubygems update command
Expand Down Expand Up @@ -983,21 +990,29 @@ func updateGems(rubyVersion string) {

if knf.GetS(GEMS_INSTALL) != "" {
for _, gem := range strings.Split(knf.GetS(GEMS_INSTALL), " ") {
var updateGemTask *Task
var gemUpdateTask *Task

gemName, gemVersion := parseGemInfo(gem)

if isGemInstalled(rubyVersion, gem) {
updateGemTask = &Task{
Desc: fmtc.Sprintf("Updating %s", gem),
if isGemInstalled(rubyVersion, gemName) {
gemUpdateTask = &Task{
Desc: fmtc.Sprintf("Updating %s", gemName),
Handler: updateGemTaskHandler,
}
} else {
updateGemTask = &Task{
Desc: fmtc.Sprintf("Installing %s", gem),
gemUpdateTask = &Task{
Desc: fmtc.Sprintf("Installing %s", gemName),
Handler: installGemTaskHandler,
}
}

installedVersion, err := updateGemTask.Start(rubyVersion, gem)
if gemVersion != "" {
gemUpdateTask.Desc += fmt.Sprintf(" (%s.x)", gemVersion)
} else {
gemUpdateTask.Desc += fmt.Sprintf(" (latest)")
}

installedVersion, err := gemUpdateTask.Start(rubyVersion, gemName, gemVersion)

if err == nil {
if installedVersion != "" {
Expand All @@ -1007,8 +1022,7 @@ func updateGems(rubyVersion string) {
)
}
} else {
fmtc.NewLine()
printErrorAndExit(err.Error())
fmtc.Printf(" {r}%v{!}\n", err)
}
}

Expand All @@ -1029,11 +1043,15 @@ func updateGems(rubyVersion string) {
}

// runGemCmd run some gem command for some version
func runGemCmd(rubyVersion, cmd, gem string) (string, error) {
func runGemCmd(rubyVersion, cmd, gem, gemVersion string) (string, error) {
start := time.Now()
rubyPath := getVersionPath(rubyVersion)
gemCmd := exec.Command(rubyPath+"/bin/ruby", rubyPath+"/bin/gem", cmd, gem)

if gemVersion != "" {
gemCmd.Args = append(gemCmd.Args, "-v", fmt.Sprintf("~>%s", gemVersion))
}

if knf.GetB(GEMS_NO_DOCUMENT) {
gemCmd.Args = append(gemCmd.Args, "--no-document")
}
Expand All @@ -1054,22 +1072,28 @@ func runGemCmd(rubyVersion, cmd, gem string) (string, error) {
return version, nil
}

if gemVersion == "" {
gemVersion = "latest"
} else {
gemVersion += ".x"
}

actionLog, err := logFailedAction(strings.TrimRight(string(output), "\r\n"))

if err == nil {
switch cmd {
case "update":
return "", fmtc.Errorf("Can't update gem %s. Gem command output saved as %s.", gem, actionLog)
return "", fmtc.Errorf("Can't update gem %s (%s). Gem command output saved as %s.", gem, gemVersion, actionLog)
default:
return "", fmtc.Errorf("Can't install gem %s. Gem command output saved as %s.", gem, actionLog)
return "", fmtc.Errorf("Can't install gem %s (%s). Gem command output saved as %s.", gem, gemVersion, actionLog)
}
}

switch cmd {
case "update":
return "", fmtc.Errorf("Can't update gem %s", gem)
return "", fmtc.Errorf("Can't update gem %s (%s)", gem, gemVersion)
default:
return "", fmtc.Errorf("Can't install gem %s", gem)
return "", fmtc.Errorf("Can't install gem %s (%s)", gem, gemVersion)
}
}

Expand Down Expand Up @@ -1488,21 +1512,31 @@ func getNameWithoutPatchLevel(name string) string {
return strings.Replace(name, "-p0", "", -1)
}

// parseGemInfo extract name and version of gem
func parseGemInfo(data string) (string, string) {
if !strings.Contains(data, "=") {
return data, ""
}

return strutil.ReadField(data, 0, false, "="),
strutil.ReadField(data, 1, false, "=")
}

// logFailedAction save data to temporary log file and return path
// to this log file
func logFailedAction(message string) (string, error) {
if len(message) == 0 {
return "", errors.New("Output data is empty")
}

tmpName := knf.GetS(MAIN_TMP_DIR) + "/" + FAIL_LOG_NAME
logSuffix := passwd.GenPassword(8, passwd.STRENGTH_WEAK)
tmpName := fmt.Sprintf("%s/rbinstall-fail-%s.log", knf.GetS(MAIN_TMP_DIR), logSuffix)

if fsutil.IsExist(tmpName) {
os.Remove(tmpName)
}

data := append([]byte(message), []byte("\n\n")...)

err := ioutil.WriteFile(tmpName, data, 0666)

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions common/rbinstall.knf
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
# If source support https this property must be set to true
source-secure: true

# List of gems installed on each ruby version
install: bundler
# List of gems to install on each Ruby version
install: bundler=1 bundler

[log]

Expand Down
6 changes: 5 additions & 1 deletion common/rbinstall.spec
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

Summary: Utility for installing prebuilt Ruby to rbenv
Name: rbinstall
Version: 0.19.3
Version: 0.20.0
Release: 0%{?dist}
Group: Applications/System
License: EKOL
Expand Down Expand Up @@ -143,6 +143,10 @@ rm -rf %{buildroot}
################################################################################

%changelog
* Wed Feb 06 2019 Anton Novojilov <[email protected]> - 0.20.0-0
- [cli] Added possibility to define versions for required gems
- [cli] Gem installation error now is not critical

* Tue Jan 22 2019 Anton Novojilov <[email protected]> - 0.19.3-0
- [cli|gen|clone] ek package updated to v10
- [cli] z7 package updated to v8
Expand Down

0 comments on commit 6f0d984

Please sign in to comment.