-
Notifications
You must be signed in to change notification settings - Fork 1
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
0 parents
commit 6ff57a0
Showing
12 changed files
with
244 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea/* | ||
build | ||
*.log |
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,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) |
9 changes: 9 additions & 0 deletions
9
cmd/CyclicCommandCheckerAndExecutive/CyclicCommandCheckerAndExecutive.go
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,9 @@ | ||
package main | ||
|
||
import "github.com/SidorkinAlex/CyclicCommandCheckerAndExecutive/internal/CliParamsParser" | ||
|
||
func main() { | ||
var cliParser = CliParamsParser.NewCliParams() | ||
cliParser.StartAction() | ||
|
||
} |
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 @@ | ||
module github.com/SidorkinAlex/CyclicCommandCheckerAndExecutive |
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,5 @@ | ||
package Actions | ||
|
||
func CreateJob() { | ||
|
||
} |
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,5 @@ | ||
package Actions | ||
|
||
func Restart() { | ||
|
||
} |
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,11 @@ | ||
package Actions | ||
|
||
import ( | ||
"github.com/SidorkinAlex/CyclicCommandCheckerAndExecutive/internal/Config" | ||
"log" | ||
) | ||
|
||
func Start() { | ||
config := Config.CreateConfig("") | ||
log.Print(config) | ||
} |
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,5 @@ | ||
package Actions | ||
|
||
func Stop() { | ||
|
||
} |
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,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() | ||
} | ||
} |
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,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 | ||
} |
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,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 | ||
} |
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 @@ | ||
0.0.1 |