From 6ff57a02e65a02b6ff322a4103266b03e3f71d4a Mon Sep 17 00:00:00 2001 From: SidAlex Date: Sat, 23 Nov 2024 13:42:22 +0300 Subject: [PATCH] start commit --- .gitignore | 3 + Makefile | 6 ++ .../CyclicCommandCheckerAndExecutive.go | 9 ++ go.mod | 1 + internal/Actions/CreateJob.go | 5 ++ internal/Actions/Restart.go | 5 ++ internal/Actions/Start.go | 11 +++ internal/Actions/Stop.go | 5 ++ internal/CliParamsParser/CliParamsParser.go | 61 +++++++++++++ internal/Config/Config.go | 52 ++++++++++++ internal/fileutils/file_utils.go | 85 +++++++++++++++++++ version | 1 + 12 files changed, 244 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 cmd/CyclicCommandCheckerAndExecutive/CyclicCommandCheckerAndExecutive.go create mode 100644 go.mod create mode 100644 internal/Actions/CreateJob.go create mode 100644 internal/Actions/Restart.go create mode 100644 internal/Actions/Start.go create mode 100644 internal/Actions/Stop.go create mode 100644 internal/CliParamsParser/CliParamsParser.go create mode 100644 internal/Config/Config.go create mode 100755 internal/fileutils/file_utils.go create mode 100644 version diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..55c6534 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea/* +build +*.log diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..079d40e --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +.PHONY: build +build: + go build -v ./cmd/CyclicCommandCheckerAndExecutive/CyclicCommandCheckerAndExecutive.go + $(eval NEW_VER:=$(shell cat version | cut -d '_' -f 2 )) + mkdir build + mv CyclicCommandCheckerAndExecutive ./build/CyclicCommandCheckerAndExecutive.$(NEW_VER) \ No newline at end of file diff --git a/cmd/CyclicCommandCheckerAndExecutive/CyclicCommandCheckerAndExecutive.go b/cmd/CyclicCommandCheckerAndExecutive/CyclicCommandCheckerAndExecutive.go new file mode 100644 index 0000000..ffe5cf9 --- /dev/null +++ b/cmd/CyclicCommandCheckerAndExecutive/CyclicCommandCheckerAndExecutive.go @@ -0,0 +1,9 @@ +package main + +import "github.com/SidorkinAlex/CyclicCommandCheckerAndExecutive/internal/CliParamsParser" + +func main() { + var cliParser = CliParamsParser.NewCliParams() + cliParser.StartAction() + +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7b6ecca --- /dev/null +++ b/go.mod @@ -0,0 +1 @@ +module github.com/SidorkinAlex/CyclicCommandCheckerAndExecutive diff --git a/internal/Actions/CreateJob.go b/internal/Actions/CreateJob.go new file mode 100644 index 0000000..03b9894 --- /dev/null +++ b/internal/Actions/CreateJob.go @@ -0,0 +1,5 @@ +package Actions + +func CreateJob() { + +} diff --git a/internal/Actions/Restart.go b/internal/Actions/Restart.go new file mode 100644 index 0000000..28b0b30 --- /dev/null +++ b/internal/Actions/Restart.go @@ -0,0 +1,5 @@ +package Actions + +func Restart() { + +} diff --git a/internal/Actions/Start.go b/internal/Actions/Start.go new file mode 100644 index 0000000..cc5e5b2 --- /dev/null +++ b/internal/Actions/Start.go @@ -0,0 +1,11 @@ +package Actions + +import ( + "github.com/SidorkinAlex/CyclicCommandCheckerAndExecutive/internal/Config" + "log" +) + +func Start() { + config := Config.CreateConfig("") + log.Print(config) +} diff --git a/internal/Actions/Stop.go b/internal/Actions/Stop.go new file mode 100644 index 0000000..1439d47 --- /dev/null +++ b/internal/Actions/Stop.go @@ -0,0 +1,5 @@ +package Actions + +func Stop() { + +} \ No newline at end of file diff --git a/internal/CliParamsParser/CliParamsParser.go b/internal/CliParamsParser/CliParamsParser.go new file mode 100644 index 0000000..66467bc --- /dev/null +++ b/internal/CliParamsParser/CliParamsParser.go @@ -0,0 +1,61 @@ +package CliParamsParser + +import ( + "flag" + "github.com/SidorkinAlex/CyclicCommandCheckerAndExecutive/internal/Actions" + "log" +) + +type CliParams struct { + Action string +} + +const ( + Start = "start" + Stop = "stop" + CreateJob = "createJob" + Restart = "restart" +) + +func NewCliParams() *CliParams { + var c CliParams + var stop bool + var start bool + var createJob bool + var restart bool + + flag.BoolVar(&stop, "stop", false, "set this param to stopping demon") + flag.BoolVar(&start, "start", false, "set this param from start program") + flag.BoolVar(&createJob, "create-job", false, "set this param from start program") + flag.BoolVar(&restart, "restart", false, "set this param from start program") + if stop { + c.Action = Start + } + if stop { + c.Action = Stop + } + if createJob { + c.Action = CreateJob + } + if restart { + c.Action = Restart + } + if c.Action == "" { + log.Fatal("To run the program, use one of the keys -stop, -start, -create-job, -restart is required.") + } + return &c +} +func (c CliParams) StartAction() { + if c.Action == Stop { + Actions.Stop() + } + if c.Action == Start { + Actions.Start() + } + if c.Action == CreateJob { + Actions.CreateJob() + } + if c.Action == Restart { + Actions.Restart() + } +} diff --git a/internal/Config/Config.go b/internal/Config/Config.go new file mode 100644 index 0000000..4ac749d --- /dev/null +++ b/internal/Config/Config.go @@ -0,0 +1,52 @@ +package Config + +import ( + "encoding/json" + "fmt" + "github.com/SidorkinAlex/CyclicCommandCheckerAndExecutive/internal/fileutils" + "log" + "os" + "path/filepath" +) + +type Config []Command + +// Config структура для хранения конфигурации +type Command struct { + Command string `json:"checkingCommand"` + Interval int `json:"interval"` + BranchCommand []BranchResultExecution +} +type BranchResultExecution struct { + ResultExecution string `json:"resultExecution"` + Commands []string `json:"commands"` +} + +func (config *Config) create(configPath string) { + +} +func CreateConfig(configPath string) Config { + var config Config + if configPath == "" { + configPath = "/etc/ContainerFailedListener/config.json" + } + dirPath := filepath.Dir(configPath) + + if _, err := os.Stat(dirPath); os.IsNotExist(err) { + err := os.MkdirAll(dirPath, os.ModePerm) + if err != nil { + log.Fatalf("Error creating config dirrectory: %v\n", err) + return nil + } + fileutils.WriteFile("[]", configPath) + return config + } + + jsonResp := fileutils.ReadFile(configPath) + err := json.Unmarshal([]byte(jsonResp), &config) + if err != nil { + fmt.Println(err) + return nil + } + return config +} diff --git a/internal/fileutils/file_utils.go b/internal/fileutils/file_utils.go new file mode 100755 index 0000000..b687d36 --- /dev/null +++ b/internal/fileutils/file_utils.go @@ -0,0 +1,85 @@ +package fileutils + +import ( + "bytes" + "fmt" + "io" + "io/ioutil" + "log" + "os" + "path/filepath" +) + +func WriteFile(mes string, filename string) { + + if !HasFile(filename) { + + file, err := os.Create(filename) + + if err != nil { + log.Println("Ошибка создания файла") + os.Exit(1) + } + defer file.Close() + file.WriteString(mes) + } else { + file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0600) + if err != nil { + log.Fatalln("error create file " + filename) + } + defer file.Close() + file.WriteString(mes) + } + +} + +func RewriteFile(mes string, filename string) { + f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755) + if err != nil { + log.Fatal(err) + } + io.WriteString(f, mes) + if err := f.Close(); err != nil { + log.Fatal(err) + } + f.Sync() +} + +func HasFile(filePath string) bool { + if _, err := os.Stat(filePath); err == nil { + return true + } + return false +} + +func ConcatString(string []string) string { + + strings := string + buffer := bytes.Buffer{} + for _, val := range strings { + buffer.WriteString(val) + } + + return buffer.String() +} + +func GetRootDir() string { + rootDir, _ := filepath.Abs(filepath.Dir(os.Args[0])) + return rootDir +} + +func ReadFile(filename string) string { + var fileContent string + if HasFile(filename) { + // для более мелких файлов + fContent, err := ioutil.ReadFile(filename) + if err != nil { + fmt.Println(err) + } + fileContent = string(fContent) + } else { + fileContent = "" + } + + return fileContent +} diff --git a/version b/version new file mode 100644 index 0000000..8a9ecc2 --- /dev/null +++ b/version @@ -0,0 +1 @@ +0.0.1 \ No newline at end of file