Skip to content

Commit

Permalink
start commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SidorkinAlex committed Nov 23, 2024
0 parents commit 6ff57a0
Show file tree
Hide file tree
Showing 12 changed files with 244 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/*
build
*.log
6 changes: 6 additions & 0 deletions Makefile
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)
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()

}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module github.com/SidorkinAlex/CyclicCommandCheckerAndExecutive
5 changes: 5 additions & 0 deletions internal/Actions/CreateJob.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package Actions

func CreateJob() {

}
5 changes: 5 additions & 0 deletions internal/Actions/Restart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package Actions

func Restart() {

}
11 changes: 11 additions & 0 deletions internal/Actions/Start.go
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)
}
5 changes: 5 additions & 0 deletions internal/Actions/Stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package Actions

func Stop() {

}
61 changes: 61 additions & 0 deletions internal/CliParamsParser/CliParamsParser.go
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()
}
}
52 changes: 52 additions & 0 deletions internal/Config/Config.go
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
}
85 changes: 85 additions & 0 deletions internal/fileutils/file_utils.go
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
}
1 change: 1 addition & 0 deletions version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.1

0 comments on commit 6ff57a0

Please sign in to comment.