Skip to content

Commit 371aa41

Browse files
committed
Added --namespace flag with input handling to agent and server
1 parent 4e7d9b7 commit 371aa41

File tree

8 files changed

+149
-58
lines changed

8 files changed

+149
-58
lines changed

cmd/agent.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,7 @@ func init() {
4343
// Cobra supports local flags which will only run when this command
4444
// is called directly, e.g.:
4545
// agentCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
46-
agentCmd.Flags().StringVar(&locker.InputArg, "file", "", "Specify path for input artifact file")
46+
agentCmd.Flags().StringVar(&locker.InputArgPath, "file", "", "Specify path for input artifact file")
47+
48+
agentCmd.Flags().StringVar(&locker.InputArgNamespace, "namespace", "", "Specify namespace in the following format: namespace/project/job-id")
4749
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ module vikingPingvin/locker
33
go 1.15
44

55
require (
6-
github.com/golang/protobuf v1.4.1
6+
github.com/golang/protobuf v1.4.3
77
github.com/mitchellh/go-homedir v1.1.0
88
github.com/rs/xid v1.2.1
99
github.com/rs/zerolog v1.20.0
1010
github.com/spf13/cobra v1.1.1
1111
github.com/spf13/viper v1.7.1
12+
golang.org/x/tools/gopls v0.5.4 // indirect
1213
google.golang.org/protobuf v1.25.0
1314
)

go.sum

Lines changed: 49 additions & 0 deletions
Large diffs are not rendered by default.

locker/agent.go

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net"
1111
"os"
1212
"path/filepath"
13+
"strings"
1314

1415
"vikingPingvin/locker/locker/messaging"
1516
"vikingPingvin/locker/locker/messaging/protobuf"
@@ -19,13 +20,17 @@ import (
1920
"google.golang.org/protobuf/proto"
2021
)
2122

22-
// FileInput String Flag for Cobra CMD input
23-
var InputArg string
23+
// InputArgPath Relative or absolute path of files for Cobra CLI
24+
var InputArgPath string
25+
var InputArgNamespace string
2426

2527
// InputData Populated at the start of the program
2628
type InputData struct {
27-
FileInput string
29+
FilePath string
2830
FileName string
31+
NameSpace string
32+
Project string
33+
JobID string
2934
FileHash []byte
3035
ID xid.ID
3136
}
@@ -61,12 +66,12 @@ func (a ArtifactAgent) Start(inputData *InputData) bool {
6166

6267
// If --file cli input is not null, parse file
6368
func parseAndSendMetaData(connection net.Conn, inputData *InputData) (fileInfo os.FileInfo, err error) {
64-
fileInfo, err = os.Stat(inputData.FileInput)
69+
fileInfo, err = os.Stat(inputData.FilePath)
6570
if os.IsNotExist(err) {
6671
log.Error().Msgf("Parsing file Input error: %v", err)
6772
return fileInfo, err
6873
}
69-
inputData.FileHash = hashFile(inputData.FileInput)
74+
inputData.FileHash = hashFile(inputData.FilePath)
7075
inputData.FileName = fileInfo.Name()
7176

7277
log.Info().
@@ -79,8 +84,9 @@ func parseAndSendMetaData(connection net.Conn, inputData *InputData) (fileInfo o
7984
message, err := messaging.CreateMessage_FileMeta(
8085
inputData.ID.Bytes(),
8186
protobuf.MessageType_META,
82-
"test_namespace",
83-
"test_project",
87+
inputData.NameSpace,
88+
inputData.Project,
89+
inputData.JobID,
8490
inputData.FileName,
8591
inputData.FileHash)
8692
if err != nil {
@@ -97,10 +103,10 @@ func parseAndSendMetaData(connection net.Conn, inputData *InputData) (fileInfo o
97103
//func parseAndSendPayload(bytes *[]byte, numBytes int) {
98104
func parseAndSendPayload(connection net.Conn, inputData *InputData) {
99105

100-
f, err := os.Open(inputData.FileInput)
106+
f, err := os.Open(inputData.FilePath)
101107
defer f.Close()
102108
if err != nil {
103-
log.Error().Msgf("Cannot open file %s", inputData.FileInput)
109+
log.Error().Msgf("Cannot open file %s", inputData.FilePath)
104110
}
105111

106112
log.Info().Msg("Started sending Payload Packets...")
@@ -195,27 +201,43 @@ func parseInputArguments() *InputData {
195201
var err error
196202
var inputPath string
197203

198-
if len(InputArg) == 0 {
204+
if len(InputArgPath) == 0 {
199205
err = errors.New("--file empty")
200206
log.Err(err).Str("agent", "parseInputArguments").Msgf("No input file was given.")
201207
}
208+
if len(InputArgNamespace) == 0 {
209+
err = errors.New("--namespace empty")
210+
log.Err(err).Str("agent", "parseInputArguments").Msgf("No input namespace was given.")
211+
}
202212
if err != nil {
203213
os.Exit(1)
204214
}
205215

206-
inputPath = InputArg
207-
if !filepath.IsAbs(InputArg) {
216+
inputPath = InputArgPath
217+
if !filepath.IsAbs(InputArgPath) {
208218
cwd, err := os.Getwd()
209219
if err != nil {
210220
log.Err(err).Msg("Error during CWD PATH parsing")
211221
os.Exit(1)
212222
}
213-
log.Debug().Msgf("Relative path of input: %s", InputArg)
214-
inputPath = filepath.Join(cwd, InputArg)
223+
log.Debug().Msgf("Relative path of input: %s", InputArgPath)
224+
inputPath = filepath.Join(cwd, InputArgPath)
215225

216226
}
227+
228+
fullNameSpace := InputArgNamespace
229+
namePaths := strings.Split(fullNameSpace, "/")
230+
if len(namePaths) != 3 {
231+
err = errors.New("Namespace must contain 3 values separated by '/'")
232+
log.Err(err).Msg("Namespace values not valid")
233+
os.Exit(1)
234+
}
235+
217236
data := &InputData{
218-
FileInput: inputPath,
237+
FilePath: inputPath,
238+
NameSpace: namePaths[0],
239+
Project: namePaths[1],
240+
JobID: namePaths[2],
219241
ID: xid.New(),
220242
}
221243
return data

locker/messaging/message_handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func CreateMessage_ServerACk(id []byte, messageType pb.MessageType, isSuccess bo
3030
// msgType
3131
// fileName
3232
// fileHash
33-
func CreateMessage_FileMeta(id []byte, msgType pb.MessageType, namesSpace string, projectName string, fileName string, fileHash []byte) (protoMessage *pb.LockerMessage, err error) {
33+
func CreateMessage_FileMeta(id []byte, msgType pb.MessageType, namesSpace string, projectName string, jobID string, fileName string, fileHash []byte) (protoMessage *pb.LockerMessage, err error) {
3434

3535
protoMessage = &pb.LockerMessage{
3636
Message: &pb.LockerMessage_Meta{
@@ -39,6 +39,7 @@ func CreateMessage_FileMeta(id []byte, msgType pb.MessageType, namesSpace string
3939
MsgType: msgType,
4040
Namespace: namesSpace,
4141
Project: projectName,
42+
JobID: jobID,
4243
Filename: fileName,
4344
Hash: fileHash,
4445
},

locker/messaging/protobuf/messageSchema.pb.go

Lines changed: 52 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

locker/messaging/protobuf/messageSchema.proto

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ message FileMeta{
1313
MessageType msgType = 2;
1414
string namespace = 3;
1515
string project = 4;
16-
string filename = 5;
17-
bytes hash = 6;
16+
string jobID = 5;
17+
string filename = 6;
18+
bytes hash = 7;
1819
}
1920

2021
message FilePackage{

locker/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ func handleConnection(connection net.Conn) {
7676

7777
metaData := metaInfo{}
7878

79-
//var hashInfoFromMeta []byte
8079
timeoutDuration := 5 * time.Second
8180
invalidCounterMax := 10
8281
invalidCounter := 0
@@ -172,6 +171,7 @@ func handleProtoMeta(metaMessage *protobuf.FileMeta) (file *os.File, metaData me
172171
Str("Artifact Name", metaMessage.GetFilename()).
173172
Str("NameSpace", metaMessage.GetNamespace()).
174173
Str("Project", metaMessage.GetProject()).
174+
Str("Job-ID", metaMessage.GetJobID()).
175175
Str("hash", fmt.Sprintf("%v", metaMessage.GetHash())).
176176
Str("id", xidValue.String()).
177177
Msg("Artifact Meta info Recieved")

0 commit comments

Comments
 (0)