-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract topaz CLI configuration package (#234)
* Extract topaz CLI configuration package * Update loader * Use loader get volumes for mount paths * Update generator * Move configuration package into topaz cc * Add config loader and generator to config package * Remove UI.Progress from cert generation
- Loading branch information
1 parent
314bbcd
commit b7f409b
Showing
12 changed files
with
469 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package config | ||
|
||
import ( | ||
"html/template" | ||
"io" | ||
"os" | ||
"path" | ||
) | ||
|
||
type Generator struct { | ||
templateParams | ||
ConfigName string | ||
} | ||
|
||
func NewGenerator(configName string) *Generator { | ||
return &Generator{ConfigName: configName} | ||
} | ||
|
||
func (g *Generator) WithVersion(version int) *Generator { | ||
g.Version = version | ||
return g | ||
} | ||
func (g *Generator) WithLocalPolicyImage(image string) *Generator { | ||
g.LocalPolicyImage = image | ||
return g | ||
} | ||
|
||
func (g *Generator) WithPolicyName(policyName string) *Generator { | ||
g.PolicyName = policyName | ||
return g | ||
} | ||
func (g *Generator) WithResource(resource string) *Generator { | ||
g.Resource = resource | ||
return g | ||
} | ||
|
||
func (g *Generator) WithEdgeDirectory(enabled bool) *Generator { | ||
g.EdgeDirectory = enabled | ||
return g | ||
} | ||
|
||
func (g *Generator) WithEnableDirectoryV2(enabled bool) *Generator { | ||
g.EnableDirectoryV2 = enabled | ||
return g | ||
} | ||
|
||
func (g *Generator) GenerateConfig(w io.Writer, templateData string) error { | ||
return g.writeConfig(w, templateData) | ||
} | ||
|
||
func (g *Generator) CreateConfigDir() (string, error) { | ||
home, err := os.UserHomeDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
configDir := path.Join(home, "/.config/topaz/cfg") | ||
if fi, err := os.Stat(configDir); err == nil && fi.IsDir() { | ||
return configDir, nil | ||
} | ||
|
||
return configDir, os.MkdirAll(configDir, 0700) | ||
} | ||
|
||
func (g *Generator) CreateCertsDir() (string, error) { | ||
home, err := os.UserHomeDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
certsDir := path.Join(home, "/.config/topaz/certs") | ||
if fi, err := os.Stat(certsDir); err == nil && fi.IsDir() { | ||
return certsDir, nil | ||
} | ||
|
||
return certsDir, os.MkdirAll(certsDir, 0700) | ||
} | ||
|
||
func (g *Generator) CreateDataDir() (string, error) { | ||
home, err := os.UserHomeDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
dataDir := path.Join(home, "/.config/topaz/db") | ||
if fi, err := os.Stat(dataDir); err == nil && fi.IsDir() { | ||
return dataDir, nil | ||
} | ||
|
||
return dataDir, os.MkdirAll(dataDir, 0700) | ||
} | ||
|
||
func (g *Generator) writeConfig(w io.Writer, templ string) error { | ||
t, err := template.New("config").Parse(templ) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = t.Execute(w, g) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.