cacert-gosignerclient/cmd/signerclient/main.go
Jan Dittberner c65853d1f9 Implement config generator
This commit adds code to allow the generation of a default client
configuration. The generator is run instead of the regular client
code, when the option -generate-config is passed on the command
line.
2022-12-03 12:22:00 +01:00

146 lines
3.5 KiB
Go

/*
Copyright 2022 CAcert Inc.
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"flag"
"fmt"
"os"
"strings"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"git.cacert.org/cacert-gosigner/pkg/protocol"
"git.cacert.org/cacert-gosignerclient/internal/client"
"git.cacert.org/cacert-gosignerclient/internal/config"
"git.cacert.org/cacert-gosignerclient/internal/handler"
)
var (
commit = "dev"
date = "unknown"
version = "unknown"
)
const (
defaultConfigFile = "config.yaml"
)
func main() {
var (
showVersion, verbose, generateConfig bool
configFile, logLevel string
logger *logrus.Logger
)
logger = logrus.New()
logger.SetOutput(os.Stdout)
logger.SetLevel(logrus.InfoLevel)
logger.Infof("cacert-gosignerclient %s (%s) - build %s", version, commit, date)
flag.StringVar(&configFile, "config", defaultConfigFile, "signer client configuration file")
flag.BoolVar(&showVersion, "version", false, "show version")
flag.BoolVar(&verbose, "verbose", false, "verbose output")
flag.BoolVar(
&generateConfig,
"generate-config",
false,
"generate a configuration file with default values instead of running the client",
)
flag.StringVar(&logLevel, "loglevel", "INFO", "log level")
flag.Parse()
if showVersion {
return
}
parsedLevel, err := logrus.ParseLevel(logLevel)
if err != nil {
logger.WithError(err).Fatal("could not parse log level")
}
logger.SetLevel(parsedLevel)
if generateConfig {
if err = generateDefaultConfig(); err != nil {
logger.WithError(err).Fatal("could not generate default configuration")
}
return
}
if err := startClient(configFile, logger); err != nil {
logger.WithError(err).Fatal("client failure")
}
}
func generateDefaultConfig() error {
const defaultBaseConfiguration = `---
serial:
device: /dev/ttyUSB0
baud: 112500
`
cfg, err := config.LoadConfiguration(strings.NewReader(
defaultBaseConfiguration))
if err != nil {
return fmt.Errorf("could not load empty configuration: %w", err)
}
enc := yaml.NewEncoder(os.Stdout)
if err = enc.Encode(cfg); err != nil {
return fmt.Errorf("could not encode: %w", err)
}
return nil
}
func startClient(configFile string, logger *logrus.Logger) error {
clientConfig, err := config.New(configFile)
if err != nil {
return fmt.Errorf("could not configure client: %w", err)
}
commands := make(chan *protocol.Command)
callbacks := make(chan interface{}, client.CallBackBufferSize)
clientHandler, err := handler.New(clientConfig, logger, commands, callbacks)
if err != nil {
return fmt.Errorf("could not setup client handler: %w", err)
}
signerClient, err := client.New(clientConfig, logger, clientHandler, commands, callbacks)
if err != nil {
return fmt.Errorf("could not setup client: %w", err)
}
defer func() { _ = signerClient.Close() }()
logger.Info("setup complete, starting client operation")
if err = signerClient.Run(context.Background()); err != nil {
return fmt.Errorf("error in client: %w", err)
}
return nil
}