Skip to content

Commit

Permalink
feat: add export library
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelncui committed Oct 6, 2023
1 parent b7c075b commit 030a6d9
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 28 deletions.
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions build_backend.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
97 changes: 97 additions & 0 deletions cmd/export-library/main.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
30 changes: 3 additions & 27 deletions cmd/httpd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand All @@ -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) })
Expand Down
File renamed without changes.
40 changes: 40 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 10 additions & 0 deletions entity/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 030a6d9

Please sign in to comment.