From ff6894f7608e895426bdc4ae2813f193971a7e6b Mon Sep 17 00:00:00 2001 From: Steffen Brinckmann Date: Mon, 6 Jan 2025 11:47:22 +0100 Subject: [PATCH 1/2] Allow to specify default parameters that are baked into the executable - build.py creates an executable --- README.md | 14 ++++++++++++-- build.py | 20 ++++++++++++++++++++ cmd_manager.go | 26 ++++++++++++++------------ default_parameter.go | 18 ++++++++++++++++++ go.mod | 2 +- 5 files changed, 65 insertions(+), 15 deletions(-) create mode 100755 build.py create mode 100644 default_parameter.go diff --git a/README.md b/README.md index 6a73567..04cf561 100755 --- a/README.md +++ b/README.md @@ -31,14 +31,24 @@ Shuttle is a utility that facilitates seamless file transfer from a source devic ## **Usage** -> **Note:** If you used the [ShuttleBuilder](https://github.com/ComPlat/shuttlebuilder), CLI arguments are preconfigured and do not need to be manually added. +### ShuttleBuilder +If you used the [ShuttleBuilder](https://github.com/ComPlat/shuttlebuilder), CLI arguments are preconfigured and do not need to be manually added. +### Execute by adding command line arguments To run the utility, execute the following command: ```shell +go build shuttle -src < source_directory_path > -duration < time_in_seconds > -dst < destination_server > -user < username > -pass < password > -type < file|folder|zip|tar|flat_tar > -transfer < webdav|sftp > -commonRegex < Regexp > ``` +### Create a executable that contains default parameters +1. Edit the default parameters in "default_parameter.go" +1. Execute build which will create an executable for the desired operating system +```shell +python build.py +``` + ### Parameters: - < source_directory_path >: Path to the source directory containing files to transfer. @@ -102,5 +112,5 @@ This project is licensed under the MIT License. - + diff --git a/build.py b/build.py new file mode 100755 index 0000000..6901b38 --- /dev/null +++ b/build.py @@ -0,0 +1,20 @@ +#!/usr/local/bin/python +import os + +# get data from go-file +with open('default_parameter.go', encoding='utf-8') as fIn: + text = fIn.read() + text = text.split('return Parameter{')[1] + lines = text.split('}')[0].split('\n') + lines = [i.strip() for i in lines if i.strip()] + data = {i.split(":")[0]:i.split(":")[1].replace('"','').replace(',','').strip() for i in lines} + +osDict = { + #label os arch extension + "linux":["linux", "amd64", "out"] , + "winXP":["windows", "386", "exe"], + "win10":["windows", "amd64", "exe"] +} +osList = osDict[data['os'].lower()] + +os.system(f"env GOOS={osList[0]} GOARCH={osList[1]} go build -o shuttle.{osList[2]}") diff --git a/cmd_manager.go b/cmd_manager.go index 8172ddc..18ccf8a 100755 --- a/cmd_manager.go +++ b/cmd_manager.go @@ -30,25 +30,27 @@ func (m Args) LogString() string { // GetCmdArgs Get/Parse command line arguments manager func GetCmdArgs() Args { - var fp, dst, user, pass, crt, durationStr, tType, name string + var fp, dst, user, pass, durationStr, tType, sendType, name string + var crt string var duration int var commonRegexStr string var commonRegex *regexp.Regexp - var sendType string var err error - flag.StringVar(&fp, "src", "{{ src }}", "Source directory to be watched.") - flag.StringVar(&name, "name", "{{ name }}", "Name of the EFW instance.") - flag.StringVar(&dst, "dst", "{{ dst }}", "WebDAV destination URL. If the destination is on the lsdf, the URL should be as follows:\nhttps://os-webdav.lsdf.kit.edu///projects//\n -Organisationseinheit, z.B. kit.\n -Institut-Name, z.B. ioc, scc, ikp, imk-asf etc.\n -User-Name z.B. xy1234, bs_abcd etc.\n -Projekt-Name") - flag.StringVar(&user, "user", "{{ user }}", "WebDAV or SFTP user") - flag.StringVar(&pass, "pass", "{{ password }}", "WebDAV or SFTP Password") - flag.StringVar(&durationStr, "duration", "{{ duration }}", "Duration in seconds, i.e., how long a file must not be changed before sent.") - flag.StringVar(&crt, "crt", "{{ crt }}", "Path to server TLS certificate. Only needed if the server has a self signed certificate.") + var defaultParameter = DefaultParameter() + + flag.StringVar(&fp, "src", defaultParameter.src, "Source directory to be watched.") + flag.StringVar(&name, "name", defaultParameter.name, "Name of the EFW instance.") + flag.StringVar(&dst, "dst", defaultParameter.dst, "WebDAV destination URL. If the destination is on the lsdf, the URL should be as follows:\nhttps://os-webdav.lsdf.kit.edu///projects//\n -Organisationseinheit, z.B. kit.\n -Institut-Name, z.B. ioc, scc, ikp, imk-asf etc.\n -User-Name z.B. xy1234, bs_abcd etc.\n -Projekt-Name") + flag.StringVar(&user, "user", defaultParameter.user, "WebDAV or SFTP user") + flag.StringVar(&pass, "pass", defaultParameter.pass, "WebDAV or SFTP Password") + flag.StringVar(&durationStr, "duration", defaultParameter.duration, "Duration in seconds, i.e., how long a file must not be changed before sent.") + flag.StringVar(&crt, "crt", "{{ crt }}", "Path to server TLS certificate. Only needed if the server has a self signed certificate.") /// Only considered if result are stored in a folder. /// If zipped is set the result folder will be transferred as zip file - flag.StringVar(&sendType, "type", "{{ type }}", "Type must be 'file', 'folder', 'tar', 'zip' or 'flat_tar'. The 'file' option means that each file is handled individually, the 'folder' option means that entire folders are transmitted only when all files in them are ready. The option 'tar' and/or 'zip' send a folder zipped, only when all files in a folder are ready. The flat_tar option packs all files with have a common prefix into a tar file in a flat folder hierarchy") - flag.StringVar(&commonRegexStr, "commonRegex", "{{ common_regex }}", "The common prefix length is only required if the type is flat_tar. This value specifies the number of leading characters that must be the same in order for files to be packed together.") - flag.StringVar(&tType, "transfer", "{{ tType }}", "Type must be 'webdav' or 'sftp'.") + flag.StringVar(&sendType, "type", defaultParameter.tType, "Type must be 'file', 'folder', 'tar', 'zip' or 'flat_tar'. The 'file' option means that each file is handled individually, the 'folder' option means that entire folders are transmitted only when all files in them are ready. The option 'tar' and/or 'zip' send a folder zipped, only when all files in a folder are ready. The flat_tar option packs all files with have a common prefix into a tar file in a flat folder hierarchy") + flag.StringVar(&commonRegexStr,"commonRegex", "{{ common_regex }}", "The common prefix length is only required if the type is flat_tar. This value specifies the number of leading characters that must be the same in order for files to be packed together.") + flag.StringVar(&tType, "transfer", defaultParameter.sendType, "Type must be 'webdav' or 'sftp'.") flag.Parse() if duration, err = strconv.Atoi(durationStr); err != nil { diff --git a/default_parameter.go b/default_parameter.go new file mode 100644 index 0000000..b2f2237 --- /dev/null +++ b/default_parameter.go @@ -0,0 +1,18 @@ +package main + +type Parameter struct { + src, user, pass, dst, sendType, tType, name, duration, os string +} + +func DefaultParameter() Parameter { + return Parameter{ + src: "", + name: "Instrument 1", + os: "winXP", + dst: "", + user: "", + pass: "", + duration: "5", + sendType: "sftp", + tType: "file"} +} \ No newline at end of file diff --git a/go.mod b/go.mod index 7bf362d..319ee77 100755 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module Shuttle -go 1.23 +go 1.23.0 require ( github.com/StarmanMartin/gowebdav v0.0.0-20220901075112-8721ee532c0c From 5d0679b1434a896517158a8fe40f7e5151092968 Mon Sep 17 00:00:00 2001 From: Steffen Brinckmann Date: Mon, 6 Jan 2025 15:54:15 +0100 Subject: [PATCH 2/2] After test of translation: py->go --- build.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/build.py b/build.py index 6901b38..3ce9f29 100755 --- a/build.py +++ b/build.py @@ -1,4 +1,5 @@ #!/usr/local/bin/python +# This cannot be in go, since you cannot execute a go command from within go import os # get data from go-file @@ -12,9 +13,10 @@ osDict = { #label os arch extension "linux":["linux", "amd64", "out"] , - "winXP":["windows", "386", "exe"], + "winxp":["windows", "386", "exe"], "win10":["windows", "amd64", "exe"] } osList = osDict[data['os'].lower()] os.system(f"env GOOS={osList[0]} GOARCH={osList[1]} go build -o shuttle.{osList[2]}") +