mirror of
https://github.com/kairos-io/provider-kairos.git
synced 2025-09-16 15:19:46 +00:00
Use FindYAMLWithKey
Signed-off-by: Ettore Di Giacinto <mudler@users.noreply.github.com>
This commit is contained in:
2
go.mod
2
go.mod
@@ -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
4
go.sum
@@ -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=
|
||||
|
@@ -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))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@@ -46,51 +46,57 @@ func RotateToken(configDir []string, newToken, apiAddress, rootDir string, resta
|
||||
}
|
||||
|
||||
func ReplaceToken(dir []string, token string) (err error) {
|
||||
c, err := config.Scan(config.Directories(dir...))
|
||||
if err != nil {
|
||||
return fmt.Errorf("no config file found: %w", err)
|
||||
}
|
||||
|
||||
header := "#node-config"
|
||||
|
||||
if hasHeader, head := config.HasHeader(c.String(), ""); hasHeader {
|
||||
header = head
|
||||
}
|
||||
|
||||
content := map[interface{}]interface{}{}
|
||||
|
||||
if err := yaml.Unmarshal([]byte(c.String()), &content); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
section, exists := content["kairos"]
|
||||
if !exists {
|
||||
return errors.New("no kairos section in config file")
|
||||
}
|
||||
|
||||
dd, err := yaml.Marshal(section)
|
||||
locations, err := config.FindYAMLWithKey("kairos.network_token", config.Directories(dir...))
|
||||
if err != nil {
|
||||
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())
|
||||
}
|
||||
|
||||
piece := map[string]interface{}{}
|
||||
header := config.DefaultHeader
|
||||
if hasHeader, head := config.HasHeader(string(dat), ""); hasHeader {
|
||||
header = head
|
||||
}
|
||||
content := map[interface{}]interface{}{}
|
||||
|
||||
if err := yaml.Unmarshal(dd, &piece); err != nil {
|
||||
return err
|
||||
if err := yaml.Unmarshal(dat, &content); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
section, exists := content["kairos"]
|
||||
if !exists {
|
||||
return errors.New("no kairos section in config file")
|
||||
}
|
||||
|
||||
dd, err := yaml.Marshal(section)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
piece := map[string]interface{}{}
|
||||
|
||||
if err := yaml.Unmarshal(dd, &piece); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
piece["network_token"] = token
|
||||
content["kairos"] = piece
|
||||
|
||||
d, err := yaml.Marshal(content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fi, err := os.Stat(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ioutil.WriteFile(f, []byte(config.AddHeader(header, string(d))), fi.Mode().Perm())
|
||||
}
|
||||
|
||||
piece["network_token"] = token
|
||||
content["kairos"] = piece
|
||||
|
||||
d, err := yaml.Marshal(content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fi, err := os.Stat(c.Location())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ioutil.WriteFile(c.Location(), []byte(config.AddHeader(header, string(d))), fi.Mode().Perm())
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user