Do a large chunk of the move to go
This commit moves a huge chunk of the code to go. The TODO.md document indicates what is left to be done.
This commit is contained in:
parent
db2ad9b4bd
commit
7b38fdd801
93 changed files with 4689 additions and 645 deletions
77
cmd/prefix.go
Normal file
77
cmd/prefix.go
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
|
||||
wisski_distillery "github.com/FAU-CDI/wisski-distillery"
|
||||
"github.com/FAU-CDI/wisski-distillery/env"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/logging"
|
||||
"github.com/tkw1536/goprogram/exit"
|
||||
)
|
||||
|
||||
// Cron is the 'cron' command
|
||||
var UpdatePrefixConfig wisski_distillery.Command = updateprefixconfig{}
|
||||
|
||||
type updateprefixconfig struct{}
|
||||
|
||||
func (updateprefixconfig) Description() wisski_distillery.Description {
|
||||
return wisski_distillery.Description{
|
||||
Requirements: env.Requirements{
|
||||
NeedsConfig: true,
|
||||
},
|
||||
Command: "update_prefix_config",
|
||||
Description: "Updates the prefix configuration",
|
||||
}
|
||||
}
|
||||
|
||||
var errPrefixUpdateFailed = exit.Error{
|
||||
Message: "Failed to update the prefix configuration: %s",
|
||||
ExitCode: exit.ExitGeneric,
|
||||
}
|
||||
|
||||
func (upc updateprefixconfig) Run(context wisski_distillery.Context) error {
|
||||
dis := context.Environment
|
||||
|
||||
instances, err := dis.AllInstances()
|
||||
if err != nil {
|
||||
return errPrefixUpdateFailed.WithMessageF(err)
|
||||
}
|
||||
|
||||
target := dis.ResolverPrefixConfig()
|
||||
|
||||
// print the configuration
|
||||
config, err := os.OpenFile(target, os.O_WRONLY, fs.ModePerm)
|
||||
if err != nil {
|
||||
return errPrefixUpdateFailed.WithMessageF(err)
|
||||
}
|
||||
|
||||
// iterate over the instances and store the last value of error
|
||||
for _, instance := range instances {
|
||||
if err := logging.LogOperation(func() error {
|
||||
// read the prefix config
|
||||
data, err := instance.PrefixConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
context.IOStream.Printf("%s", data)
|
||||
|
||||
// and write it out!
|
||||
if _, err := config.WriteString(data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}, context.IOStream, "reading prefix config %s", instance.Slug); err != nil {
|
||||
return errPrefixUpdateFailed.WithMessageF(err)
|
||||
}
|
||||
}
|
||||
|
||||
// and restart the resolver to apply the config!
|
||||
logging.LogMessage(context.IOStream, "restarting resolver stack")
|
||||
if err := dis.ResolverStack().Restart(context.IOStream); err != nil {
|
||||
return errPrefixUpdateFailed.WithMessageF(err)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue