Use FindYAMLWithKey

Signed-off-by: Ettore Di Giacinto <mudler@users.noreply.github.com>
This commit is contained in:
Ettore Di Giacinto
2022-11-28 12:46:15 +00:00
parent 3eccf55e23
commit 06556a31ec
4 changed files with 53 additions and 47 deletions

2
go.mod
View File

@@ -9,7 +9,7 @@ require (
github.com/gliderlabs/ssh v0.2.2
github.com/google/go-containerregistry v0.11.0
github.com/ipfs/go-log v1.0.5
github.com/kairos-io/kairos v1.2.1
github.com/kairos-io/kairos v1.24.3-56.0.20221128123446-ab088839ec95
github.com/mudler/edgevpn v0.15.3
github.com/mudler/go-nodepair v0.0.0-20220507212557-7d47aa3cc1f1
github.com/mudler/go-pluggable v0.0.0-20220716112424-189d463e3ff3

4
go.sum
View File

@@ -749,8 +749,8 @@ github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7V
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/julz/importas v0.0.0-20210419104244-841f0c0fe66d/go.mod h1:oSFU2R4XK/P7kNBrnL/FEQlDGN1/6WoxXEjSSXO0DV0=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
github.com/kairos-io/kairos v1.2.1 h1:lDUbD8tBpHwOovLuqpS8WKPOgqN6BwUFqmJitk3W64s=
github.com/kairos-io/kairos v1.2.1/go.mod h1:Rgn/1YTvcTQIdtzvKT96FoMH8tdGiYI9tZwcu1CV1dI=
github.com/kairos-io/kairos v1.24.3-56.0.20221128123446-ab088839ec95 h1:dSTM/gePf/Czu7eOKwvv3J91AWMxH/Y4xOGVQNMu+OY=
github.com/kairos-io/kairos v1.24.3-56.0.20221128123446-ab088839ec95/go.mod h1:Rgn/1YTvcTQIdtzvKT96FoMH8tdGiYI9tZwcu1CV1dI=
github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d/go.mod h1:P2viExyCEfeWGU259JnaQ34Inuec4R38JCyBx2edgD0=
github.com/kbinani/screenshot v0.0.0-20210720154843-7d3a670d8329 h1:qq2nCpSrXrmvDGRxW0ruW9BVEV1CN2a9YDOExdt+U0o=
github.com/kbinani/screenshot v0.0.0-20210720154843-7d3a670d8329/go.mod h1:2VPVQDR4wO7KXHwP+DAypEy67rXf+okUx2zjgpCxZw4=

View File

@@ -36,7 +36,7 @@ bb:
err := ioutil.WriteFile(filepath.Join(d, "test"), []byte(cc), os.ModePerm)
Expect(err).ToNot(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(d, "b"), []byte(`
fooz:
fooz: "bar"
`), os.ModePerm)
Expect(err).ToNot(HaveOccurred())
@@ -50,14 +50,14 @@ fooz:
err = yaml.Unmarshal(content, &res)
Expect(err).ToNot(HaveOccurred())
hasHeader, _ := config.HasHeader(string(content), "#node-config")
Expect(hasHeader).To(BeTrue())
Expect(res).To(Equal(map[interface{}]interface{}{
"kairos": map[interface{}]interface{}{"network_token": "baz"},
"bb": map[interface{}]interface{}{"nothing": "foo"},
}))
})
hasHeader, _ := config.HasHeader(string(content), "#node-config")
Expect(hasHeader).To(BeTrue(), string(content))
})
})
})

View File

@@ -46,20 +46,23 @@ func RotateToken(configDir []string, newToken, apiAddress, rootDir string, resta
}
func ReplaceToken(dir []string, token string) (err error) {
c, err := config.Scan(config.Directories(dir...))
locations, err := config.FindYAMLWithKey("kairos.network_token", config.Directories(dir...))
if err != nil {
return fmt.Errorf("no config file found: %w", err)
return err
}
for _, f := range locations {
dat, err := os.ReadFile(f)
if err != nil {
fmt.Printf("warning: could not read %s '%s'\n", f, err.Error())
}
header := "#node-config"
if hasHeader, head := config.HasHeader(c.String(), ""); hasHeader {
header := config.DefaultHeader
if hasHeader, head := config.HasHeader(string(dat), ""); hasHeader {
header = head
}
content := map[interface{}]interface{}{}
if err := yaml.Unmarshal([]byte(c.String()), &content); err != nil {
if err := yaml.Unmarshal(dat, &content); err != nil {
return err
}
@@ -87,10 +90,13 @@ func ReplaceToken(dir []string, token string) (err error) {
return err
}
fi, err := os.Stat(c.Location())
fi, err := os.Stat(f)
if err != nil {
return err
}
return ioutil.WriteFile(c.Location(), []byte(config.AddHeader(header, string(d))), fi.Mode().Perm())
return ioutil.WriteFile(f, []byte(config.AddHeader(header, string(d))), fi.Mode().Perm())
}
return nil
}