-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7c075b
commit 030a6d9
Showing
7 changed files
with
152 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters