grim/hgkeeper

rework the ssh commands so there have a setup and teardown functions
//go:generate esc -o resources.go -pkg setup -include resources\/.+ -prefix resources/ .
package setup
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"bitbucket.org/rw_grim/hgkeeper/globals"
"bitbucket.org/rw_grim/hgkeeper/hg"
)
type Command struct{}
var (
hgUsername = "hgkeeper"
commitMessage = "initial revision"
)
func runCmd(cmd *hg.Command) error {
output, err := cmd.Cmd().CombinedOutput()
if len(output) > 0 {
fmt.Printf("%s\n", output)
}
return err
}
func (c *Command) createAdminRepo(reposPath, adminRepo string) error {
path := filepath.Join(reposPath, adminRepo)
// create the admin repo
if err := runCmd(hg.Init(path)); err != nil {
return err
}
filenames := []string{}
// walk through the embeded resources and dump them into the directory
for name, data := range _escData {
// skip directories as we only add the ones were we have files
if data.isDir {
continue
}
// we're copying a regular file now, so figure out the paths so we can
// create them if necessary.
absname := filepath.Join(path, name)
dirname := filepath.Dir(absname)
// if we don't have the directory create it
if _, err := os.Stat(dirname); err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(dirname, 0755); err != nil {
return err
}
} else {
return err
}
}
if err := ioutil.WriteFile(absname, FSMustByte(false, name), 0644); err != nil {
return err
}
rel, err := filepath.Rel(path, absname)
if err != nil {
return err
}
if !strings.HasPrefix(rel, ".hg/") {
filenames = append(filenames, rel)
}
}
// add our files
if err := runCmd(hg.Add(path, filenames...)); err != nil {
return err
}
// commit our changes
if err := runCmd(hg.Commit(path, hgUsername, commitMessage)); err != nil {
return err
}
return nil
}
func (c *Command) Run(g *globals.Globals) error {
if err := c.createAdminRepo(g.ReposPath, g.AdminRepo); err != nil {
// do clean up
return err
}
return nil
}