tiktok-dl/models/config/config.go

85 lines
2.3 KiB
Go
Raw Permalink Normal View History

package config
2020-01-19 02:11:53 +00:00
import (
2020-02-24 23:01:10 +00:00
"flag"
"fmt"
"os"
"strconv"
res "github.com/pikami/tiktok-dl/resources"
2020-01-19 02:11:53 +00:00
)
// Config - Runtime configuration
var Config struct {
URL string
OutputPath string
BatchFilePath string
ScrapedDataFilePath string
ArchiveFilePath string
FailLogFilePath string
Debug bool
MetaData bool
Quiet bool
JSONOnly bool
Deadline int
Limit int
2020-01-19 02:11:53 +00:00
}
// GetConfig - Returns Config object
func GetConfig() {
outputPath := flag.String(res.OutputFlag, res.OutputDefault, res.OutputDescription)
batchFilePath := flag.String(res.BatchFlag, res.BatchDefault, res.BatchDescription)
scrapedDataFilePath := flag.String(res.ScrapedDataFlag, res.ScrapedDataDefault, res.ScrapedDataDescription)
archive := flag.String(res.ArchiveFlag, res.ArchiveDefault, res.ArchiveDescription)
failLogPath := flag.String(res.FailLogFlag, res.FailLogDefault, res.FailLogDescription)
debug := flag.Bool(res.DebugFlag, parseBool(res.DebugDefault), res.DebugDescription)
metadata := flag.Bool(res.MetadataFlag, parseBool(res.MetadataDefault), res.MetadataDescription)
quiet := flag.Bool(res.QuietFlag, parseBool(res.QuietDefault), res.QuietDescription)
jsonOnly := flag.Bool(res.JsonFlag, parseBool(res.JsonDefault), res.JsonDescription)
deadline := flag.Int(res.DeadlineFlag, parseInt(res.DeadlineDefault), res.DeadlineDescription)
limit := flag.Int(res.LimitFlag, parseInt(res.LimitDefault), res.LimitDescription)
2020-02-24 23:01:10 +00:00
flag.Parse()
2020-01-19 02:11:53 +00:00
2020-02-24 23:01:10 +00:00
args := flag.Args()
if len(args) < 1 && *batchFilePath == "" && *scrapedDataFilePath == "" {
fmt.Println(res.UsageLine)
2020-02-24 23:01:10 +00:00
os.Exit(2)
}
2020-01-19 02:11:53 +00:00
2020-02-24 23:01:10 +00:00
if len(args) > 0 {
Config.URL = flag.Args()[len(args)-1]
} else {
Config.URL = ""
}
Config.OutputPath = *outputPath
Config.BatchFilePath = *batchFilePath
Config.ScrapedDataFilePath = *scrapedDataFilePath
2020-03-22 00:10:24 +00:00
Config.ArchiveFilePath = *archive
Config.FailLogFilePath = *failLogPath
2020-02-24 23:01:10 +00:00
Config.Debug = *debug
Config.MetaData = *metadata
Config.Quiet = *quiet
if *jsonOnly {
Config.Quiet = true
}
2020-03-22 00:10:24 +00:00
Config.JSONOnly = *jsonOnly
2020-02-24 23:01:10 +00:00
Config.Deadline = *deadline
Config.Limit = *limit
2020-01-19 02:11:53 +00:00
}
func parseBool(str string) bool {
val, err := strconv.ParseBool(str)
if err != nil {
panic(err)
}
return val
}
func parseInt(str string) int {
val, err := strconv.Atoi(str)
if err != nil {
panic(err)
}
return val
}