Skip to content

Commit

Permalink
fixes #112
Browse files Browse the repository at this point in the history
  • Loading branch information
ivotron committed Sep 9, 2017
1 parent b72b2d5 commit 7c50388
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 301 deletions.
72 changes: 72 additions & 0 deletions popper/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"fmt"
"log"
"path"
"strings"

sh "github.com/codeskyblue/go-sh"
"github.com/spf13/cobra"
)

func getRepoInfo() (user, repo string, err error) {
remoteURL, err := sh.Command(
"git", "config", "--get", "remote.origin.url").Output()
if err != nil {
return
}
urlAndUser, repo := path.Split(string(remoteURL))

// get the user or org name
user = path.Base(strings.Replace(urlAndUser, ":", "/", -1))

// trim and remove .git extension, if present
repo = strings.TrimSuffix(strings.TrimSpace(repo), ".git")

return
}

func addExperiment(templateType string, templateName string, folder string) {
checkTemplateFolderExists(templateType)

if sh.Test("dir", folder) {
log.Fatalln("Folder " + folder + " already exists.")
}

template := popperFolder + "/templates/" + templateType + "/" + templateName

if _, err := sh.Command("cp", "-r", template, folder).CombinedOutput(); err != nil {
log.Fatalln(err)
}
}

var addCmd = &cobra.Command{
Use: "add <experiment> [<name>]",
Short: "Add an experiment to the project",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
expname := ""
if len(args) == 1 {
expname = args[0]
} else if len(args) == 2 {
expname = args[1]
} else {
log.Fatalln("Incorrect number of arguments, type 'popper experiment add --help'")
}
if !sh.Test("dir", ".git") {
log.Fatalln("Can't find .git folder. Are you on the root folder of project?")
}
if strings.HasPrefix(args[0], "paper-") {
addExperiment("paper", args[0], "paper/")
} else {
addExperiment("experiments", args[0], "experiments/"+expname)
}

fmt.Println("Added " + expname + " to experiments/ folder.")
},
}

func init() {
RootCmd.AddCommand(addCmd)
}
218 changes: 0 additions & 218 deletions popper/experiment.go

This file was deleted.

38 changes: 38 additions & 0 deletions popper/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"log"

sh "github.com/codeskyblue/go-sh"
"github.com/spf13/cobra"
)

func checkTemplateFolderExists(templateType string) {
if !sh.Test("dir", popperFolder+"/templates/"+templateType) {
log.Fatalln("Can't find '" + popperFolder + "/templates/" + templateType + "'." +
"This command must be executed from the project's root folder.")
}
}

func showExperimentInfo(experimentName string) {
checkTemplateFolderExists("experiments")
if err := sh.Command("cat", popperFolder+"/templates/experiments/"+experimentName+"/README.md").Run(); err != nil {
log.Fatalln(err)
}
}

var infoCmd = &cobra.Command{
Use: "info <name>",
Short: "Show information about an experiment.",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
log.Fatalln("This command takes name of experiment as argument.")
}
showExperimentInfo(args[0])
},
}

func init() {
RootCmd.AddCommand(infoCmd)
}

0 comments on commit 7c50388

Please sign in to comment.