grim/hgkeeper

Parents df734a54a97c
Children 285592accfa6
convert the setup command to use the stdlib embed package instead of esc
--- a/setup/command.go Sun Mar 06 23:52:21 2022 -0600
+++ b/setup/command.go Mon Mar 07 01:07:26 2022 -0600
@@ -1,8 +1,9 @@
-//go:generate esc -o resources.go -pkg setup -include resources\/.+ -prefix resources/ .
package setup
import (
+ "embed"
"fmt"
+ "io/fs"
"io/ioutil"
"os"
"path/filepath"
@@ -20,6 +21,9 @@
AdminSSHPubkey string `kong:"flag,name='admin-pubkey',env='HGK_ADMIN_PUBKEY',help='the path to the ssh pubkey to use for the admin',required='true',type='existingfile'"`
}
+//go:embed resources/*
+var resources embed.FS
+
var (
hgUsername = "hgkeeper"
@@ -36,32 +40,34 @@
}
func (c *Command) createAdminRepo(reposPath, adminRepo string) error {
- path := filepath.Join(reposPath, adminRepo)
+ adminPath := filepath.Join(reposPath, adminRepo)
// create the admin repo
- if err := runCmd(hg.Init(path)); err != nil {
+ if err := runCmd(hg.Init(adminPath)); err != nil {
return err
}
filenames := []string{}
- // walk through the embedded 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
+ err := fs.WalkDir(resources, "resources", func(path string, entry fs.DirEntry, err error) error {
+ if err != nil {
+ return err
+ }
+
+ if entry.IsDir() {
+ return nil
}
- // we're copying a regular file now, so figure out the paths so we can
- // create them if necessary. We have to special case dothg because
- // using .hg causes issues
- var absname string
- if strings.HasPrefix(name, "/dothg/") {
- absname = filepath.Join(path, ".hg", name[7:])
- } else {
- absname = filepath.Join(path, name)
- }
+ // Remove the resources prefix.
+ stripped := strings.TrimPrefix(path, "resources")
+ // Figure out the absolute path of the target file.
+ absname := filepath.Join(adminPath, stripped)
+
+ // We can't start a name with . so we have to manually replace it.
+ absname = strings.Replace(absname, "/dothg", "/.hg", 1)
+
+ // We need to know the directory name so we can create it.
dirname := filepath.Dir(absname)
// if we don't have the directory create it
@@ -76,10 +82,15 @@
}
// If this is a template file, execute it
- if filepath.Ext(name) == ".template" {
- absname = absname[:len(absname)-len(".template")]
+ if filepath.Ext(absname) == ".template" {
+ absname = strings.TrimSuffix(absname, ".template")
- tplate, err := template.New(name).Parse(FSMustString(false, name))
+ data, err := resources.ReadFile(path)
+ if err != nil {
+ return err
+ }
+
+ tplate, err := template.New(path).Parse(string(data))
if err != nil {
return err
}
@@ -104,12 +115,18 @@
}
} else {
// If it's not a template just write it out
- if err := ioutil.WriteFile(absname, FSMustByte(false, name), 0644); err != nil {
+ data, err := resources.ReadFile(path)
+ if err != nil {
+ return err
+ }
+
+ if err := ioutil.WriteFile(absname, data, 0644); err != nil {
return err
}
}
- rel, err := filepath.Rel(path, absname)
+ // Figure out which files need to be committed to the repository.
+ rel, err := filepath.Rel(adminPath, absname)
if err != nil {
return err
}
@@ -117,10 +134,15 @@
if !strings.HasPrefix(rel, fmt.Sprintf(".hg%c", filepath.Separator)) {
filenames = append(filenames, rel)
}
+
+ return nil
+ })
+ if err != nil {
+ return err
}
// create our keys directory
- keysDir := filepath.Join(path, "keys")
+ keysDir := filepath.Join(adminPath, "keys")
if err := os.MkdirAll(keysDir, 0755); err != nil {
return err
}
@@ -132,7 +154,7 @@
}
adminPubkey := filepath.Join(keysDir, c.AdminUsername)
- relAdminPubkey, err := filepath.Rel(path, adminPubkey)
+ relAdminPubkey, err := filepath.Rel(adminPath, adminPubkey)
if err != nil {
return err
}
@@ -143,12 +165,12 @@
}
// add our files
- if err := runCmd(hg.Add(path, filenames...)); err != nil {
+ if err := runCmd(hg.Add(adminPath, filenames...)); err != nil {
return err
}
// commit our changes
- if err := runCmd(hg.Commit(path, hgUsername, commitMessage)); err != nil {
+ if err := runCmd(hg.Commit(adminPath, hgUsername, commitMessage)); err != nil {
return err
}