feat: add export library

This commit is contained in:
Samuel N Cui
2023-10-06 09:04:10 +08:00
parent b7c075b8a5
commit 030a6d9516
7 changed files with 152 additions and 28 deletions

View File

@@ -12,7 +12,7 @@ mkdir -p output/captured_indices;
cp -r scripts ./output/;
cp ./cmd/httpd/yatm-httpd.service ./output/
cp ./cmd/httpd/config.example.yaml ./output/
cp ./config.example.yaml ./output/
cp ./LICENSE ./output/
cp ./README.md ./output/
echo "${RELEASE_VERSION}" > ./output/VERSION

View File

@@ -5,4 +5,5 @@ CURDIR=$(cd $(dirname $0); pwd);
cd ${CURDIR};
go build -o ./output/yatm-httpd ./cmd/httpd;
go build -o ./output/yatm-export-library ./cmd/export-library;
go build -o ./output/yatm-lto-info ./cmd/lto-info;

View File

@@ -0,0 +1,97 @@
package main
import (
"context"
"flag"
"fmt"
"io"
"os"
"strings"
"time"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/rifflock/lfshook"
"github.com/samuelncui/yatm/config"
"github.com/samuelncui/yatm/entity"
"github.com/samuelncui/yatm/library"
"github.com/samuelncui/yatm/resource"
"github.com/sirupsen/logrus"
)
var (
configOpt = flag.String("config", "./config.yaml", "config file path")
typesOpt = flag.String("types", "file,tape,position", "types wants to be exported")
outputOpt = flag.String("output", "stdout", "output file path, default use stdout")
)
func main() {
ctx := context.Background()
logWriter, err := rotatelogs.New(
"./run.log.%Y%m%d%H%M",
rotatelogs.WithLinkName("./run.log"),
rotatelogs.WithMaxAge(time.Duration(86400)*time.Second),
rotatelogs.WithRotationTime(time.Duration(604800)*time.Second),
)
if err != nil {
panic(err)
}
logrus.AddHook(lfshook.NewHook(
lfshook.WriterMap{
logrus.InfoLevel: logWriter,
logrus.ErrorLevel: logWriter,
},
&logrus.TextFormatter{},
))
flag.Parse()
conf := config.GetConfig(*configOpt)
db, err := resource.NewDBConn(conf.Database.Dialect, conf.Database.DSN)
if err != nil {
panic(err)
}
lib := library.New(db)
if err := lib.AutoMigrate(); err != nil {
panic(err)
}
parts := strings.Split(*typesOpt, ",")
toEnum := entity.ToEnum(entity.LibraryEntityType_value, entity.LibraryEntityType_NONE)
types := make([]entity.LibraryEntityType, 0, len(parts))
for _, part := range parts {
e := toEnum(strings.ToUpper(strings.TrimSpace(part)))
if e == entity.LibraryEntityType_NONE {
continue
}
types = append(types, e)
}
if len(types) == 0 {
panic(fmt.Errorf("cannot found types, use 'types' option to specify at least one type"))
}
jsonBuf, err := lib.Export(ctx, types)
if err != nil {
panic(fmt.Errorf("export json fail, %w", err))
}
f := func() io.WriteCloser {
if *outputOpt == "stdout" {
return os.Stdout
}
f, err := os.Create(*outputOpt)
if err != nil {
panic(fmt.Errorf("open output file fail, path= '%s', %w", *outputOpt, err))
}
return f
}()
defer f.Close()
if _, err := f.Write(jsonBuf); err != nil {
panic(fmt.Errorf("write output file fail, %w", err))
}
}

View File

@@ -16,6 +16,7 @@ import (
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/rifflock/lfshook"
"github.com/samuelncui/yatm/apis"
"github.com/samuelncui/yatm/config"
"github.com/samuelncui/yatm/entity"
"github.com/samuelncui/yatm/executor"
"github.com/samuelncui/yatm/library"
@@ -25,26 +26,10 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"gopkg.in/yaml.v2"
)
type config struct {
Domain string `yaml:"domain"`
Listen string `yaml:"listen"`
DebugListen string `yaml:"debug_listen"`
Database struct {
Dialect string `yaml:"dialect"`
DSN string `yaml:"dsn"`
} `yaml:"database"`
Paths executor.Paths `yaml:"paths"`
TapeDevices []string `yaml:"tape_devices"`
Scripts executor.Scripts `yaml:"scripts"`
}
var (
configPath = flag.String("config", "./config.yaml", "config file path")
configOpt = flag.String("config", "./config.yaml", "config file path")
)
func main() {
@@ -66,16 +51,7 @@ func main() {
))
flag.Parse()
cf, err := os.Open(*configPath)
if err != nil {
panic(err)
}
conf := new(config)
if err := yaml.NewDecoder(cf).Decode(conf); err != nil {
panic(err)
}
logrus.Infof("read config success, conf= '%+v'", conf)
conf := config.GetConfig(*configOpt)
if conf.DebugListen != "" {
go tools.Wrap(context.Background(), func() { tools.NewDebugServer(conf.DebugListen) })

40
config/config.go Normal file
View File

@@ -0,0 +1,40 @@
package config
import (
"fmt"
"os"
"github.com/samuelncui/yatm/executor"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
type Config struct {
Domain string `yaml:"domain"`
Listen string `yaml:"listen"`
DebugListen string `yaml:"debug_listen"`
Database struct {
Dialect string `yaml:"dialect"`
DSN string `yaml:"dsn"`
} `yaml:"database"`
Paths executor.Paths `yaml:"paths"`
TapeDevices []string `yaml:"tape_devices"`
Scripts executor.Scripts `yaml:"scripts"`
}
func GetConfig(path string) *Config {
cf, err := os.Open(path)
if err != nil {
panic(fmt.Errorf("open config file failed, %w", err))
}
conf := new(Config)
if err := yaml.NewDecoder(cf).Decode(conf); err != nil {
panic(fmt.Errorf("decode config file failed, %w", err))
}
logrus.Infof("read config success, conf= '%+v'", conf)
return conf
}

View File

@@ -104,3 +104,13 @@ func Value(src proto.Message) (driver.Value, error) {
return buf, nil
}
func ToEnum[T ~int32](pbmap map[string]int32, default_ T) func(str string) T {
return func(str string) T {
v, ok := pbmap[string(str)]
if !ok {
return default_
}
return T(v)
}
}