Skip to content

Commit 030a6d9

Browse files
committed
feat: add export library
1 parent b7c075b commit 030a6d9

File tree

7 files changed

+152
-28
lines changed

7 files changed

+152
-28
lines changed

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mkdir -p output/captured_indices;
1212

1313
cp -r scripts ./output/;
1414
cp ./cmd/httpd/yatm-httpd.service ./output/
15-
cp ./cmd/httpd/config.example.yaml ./output/
15+
cp ./config.example.yaml ./output/
1616
cp ./LICENSE ./output/
1717
cp ./README.md ./output/
1818
echo "${RELEASE_VERSION}" > ./output/VERSION

build_backend.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ CURDIR=$(cd $(dirname $0); pwd);
55
cd ${CURDIR};
66

77
go build -o ./output/yatm-httpd ./cmd/httpd;
8+
go build -o ./output/yatm-export-library ./cmd/export-library;
89
go build -o ./output/yatm-lto-info ./cmd/lto-info;

cmd/export-library/main.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"flag"
6+
"fmt"
7+
"io"
8+
"os"
9+
"strings"
10+
"time"
11+
12+
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
13+
"github.com/rifflock/lfshook"
14+
"github.com/samuelncui/yatm/config"
15+
"github.com/samuelncui/yatm/entity"
16+
"github.com/samuelncui/yatm/library"
17+
"github.com/samuelncui/yatm/resource"
18+
"github.com/sirupsen/logrus"
19+
)
20+
21+
var (
22+
configOpt = flag.String("config", "./config.yaml", "config file path")
23+
typesOpt = flag.String("types", "file,tape,position", "types wants to be exported")
24+
outputOpt = flag.String("output", "stdout", "output file path, default use stdout")
25+
)
26+
27+
func main() {
28+
ctx := context.Background()
29+
30+
logWriter, err := rotatelogs.New(
31+
"./run.log.%Y%m%d%H%M",
32+
rotatelogs.WithLinkName("./run.log"),
33+
rotatelogs.WithMaxAge(time.Duration(86400)*time.Second),
34+
rotatelogs.WithRotationTime(time.Duration(604800)*time.Second),
35+
)
36+
if err != nil {
37+
panic(err)
38+
}
39+
logrus.AddHook(lfshook.NewHook(
40+
lfshook.WriterMap{
41+
logrus.InfoLevel: logWriter,
42+
logrus.ErrorLevel: logWriter,
43+
},
44+
&logrus.TextFormatter{},
45+
))
46+
47+
flag.Parse()
48+
conf := config.GetConfig(*configOpt)
49+
50+
db, err := resource.NewDBConn(conf.Database.Dialect, conf.Database.DSN)
51+
if err != nil {
52+
panic(err)
53+
}
54+
55+
lib := library.New(db)
56+
if err := lib.AutoMigrate(); err != nil {
57+
panic(err)
58+
}
59+
60+
parts := strings.Split(*typesOpt, ",")
61+
62+
toEnum := entity.ToEnum(entity.LibraryEntityType_value, entity.LibraryEntityType_NONE)
63+
types := make([]entity.LibraryEntityType, 0, len(parts))
64+
for _, part := range parts {
65+
e := toEnum(strings.ToUpper(strings.TrimSpace(part)))
66+
if e == entity.LibraryEntityType_NONE {
67+
continue
68+
}
69+
70+
types = append(types, e)
71+
}
72+
if len(types) == 0 {
73+
panic(fmt.Errorf("cannot found types, use 'types' option to specify at least one type"))
74+
}
75+
76+
jsonBuf, err := lib.Export(ctx, types)
77+
if err != nil {
78+
panic(fmt.Errorf("export json fail, %w", err))
79+
}
80+
81+
f := func() io.WriteCloser {
82+
if *outputOpt == "stdout" {
83+
return os.Stdout
84+
}
85+
86+
f, err := os.Create(*outputOpt)
87+
if err != nil {
88+
panic(fmt.Errorf("open output file fail, path= '%s', %w", *outputOpt, err))
89+
}
90+
return f
91+
}()
92+
93+
defer f.Close()
94+
if _, err := f.Write(jsonBuf); err != nil {
95+
panic(fmt.Errorf("write output file fail, %w", err))
96+
}
97+
}

cmd/httpd/main.go

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
1717
"github.com/rifflock/lfshook"
1818
"github.com/samuelncui/yatm/apis"
19+
"github.com/samuelncui/yatm/config"
1920
"github.com/samuelncui/yatm/entity"
2021
"github.com/samuelncui/yatm/executor"
2122
"github.com/samuelncui/yatm/library"
@@ -25,26 +26,10 @@ import (
2526
"google.golang.org/grpc"
2627
"google.golang.org/grpc/codes"
2728
"google.golang.org/grpc/status"
28-
"gopkg.in/yaml.v2"
2929
)
3030

31-
type config struct {
32-
Domain string `yaml:"domain"`
33-
Listen string `yaml:"listen"`
34-
DebugListen string `yaml:"debug_listen"`
35-
36-
Database struct {
37-
Dialect string `yaml:"dialect"`
38-
DSN string `yaml:"dsn"`
39-
} `yaml:"database"`
40-
41-
Paths executor.Paths `yaml:"paths"`
42-
TapeDevices []string `yaml:"tape_devices"`
43-
Scripts executor.Scripts `yaml:"scripts"`
44-
}
45-
4631
var (
47-
configPath = flag.String("config", "./config.yaml", "config file path")
32+
configOpt = flag.String("config", "./config.yaml", "config file path")
4833
)
4934

5035
func main() {
@@ -66,16 +51,7 @@ func main() {
6651
))
6752

6853
flag.Parse()
69-
cf, err := os.Open(*configPath)
70-
if err != nil {
71-
panic(err)
72-
}
73-
74-
conf := new(config)
75-
if err := yaml.NewDecoder(cf).Decode(conf); err != nil {
76-
panic(err)
77-
}
78-
logrus.Infof("read config success, conf= '%+v'", conf)
54+
conf := config.GetConfig(*configOpt)
7955

8056
if conf.DebugListen != "" {
8157
go tools.Wrap(context.Background(), func() { tools.NewDebugServer(conf.DebugListen) })

config/config.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/samuelncui/yatm/executor"
8+
"github.com/sirupsen/logrus"
9+
"gopkg.in/yaml.v2"
10+
)
11+
12+
type Config struct {
13+
Domain string `yaml:"domain"`
14+
Listen string `yaml:"listen"`
15+
DebugListen string `yaml:"debug_listen"`
16+
17+
Database struct {
18+
Dialect string `yaml:"dialect"`
19+
DSN string `yaml:"dsn"`
20+
} `yaml:"database"`
21+
22+
Paths executor.Paths `yaml:"paths"`
23+
TapeDevices []string `yaml:"tape_devices"`
24+
Scripts executor.Scripts `yaml:"scripts"`
25+
}
26+
27+
func GetConfig(path string) *Config {
28+
cf, err := os.Open(path)
29+
if err != nil {
30+
panic(fmt.Errorf("open config file failed, %w", err))
31+
}
32+
33+
conf := new(Config)
34+
if err := yaml.NewDecoder(cf).Decode(conf); err != nil {
35+
panic(fmt.Errorf("decode config file failed, %w", err))
36+
}
37+
38+
logrus.Infof("read config success, conf= '%+v'", conf)
39+
return conf
40+
}

entity/utils.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,13 @@ func Value(src proto.Message) (driver.Value, error) {
104104

105105
return buf, nil
106106
}
107+
108+
func ToEnum[T ~int32](pbmap map[string]int32, default_ T) func(str string) T {
109+
return func(str string) T {
110+
v, ok := pbmap[string(str)]
111+
if !ok {
112+
return default_
113+
}
114+
return T(v)
115+
}
116+
}

0 commit comments

Comments
 (0)