8
[go] Feedback for my first project: reimplement pulp-manifest
(programming.dev)
Hi ๐
I have tried to learn some go but I am still very much at the beginning, like understanding how to work with variables and functions. My background is mostly in python but I am not a programmer by trade.
package main
import (
"crypto/sha256"
"fmt"
"io"
"log"
"os"
"path/filepath"
)
func write_lines_to_file(lines []string, output_file string) {
f, err2 := os.Create(output_file)
if err2 != nil {
log.Fatal(err2)
}
defer f.Close()
for _, line := range lines {
_, err := f.WriteString(line + "\n")
if err != nil {
log.Fatal(err2)
}
}
}
func get_size_and_hash(file_path string) (int, string) {
file, err := os.Open(file_path)
if err != nil {
panic(err)
}
defer file.Close()
hash := sha256.New()
if _, err := io.Copy(hash, file); err != nil {
panic(err)
}
sum := fmt.Sprintf("%x", hash.Sum(nil))
file, err2 := os.Open(file_path)
if err2 != nil {
log.Fatal(err2)
}
fi, err2 := file.Stat()
if err != nil {
log.Fatal(err2)
}
my_size := fi.Size()
return int(my_size), string(sum)
}
func get_list_of_files(target_directory string) []string {
var files []string
err := filepath.Walk(target_directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println(err)
return nil
}
if !info.IsDir() {
files = append(files, path)
}
return nil
})
if err != nil {
log.Fatal(err)
}
return files
}
func main() {
// accept directory as user input
target_directory := os.Args[1]
my_files := get_list_of_files(target_directory)
var content []string
for _, file := range my_files {
size, hash := get_size_and_hash(file)
var str_file string = string(file)
str_size := fmt.Sprint(size)
var str_hash string = string(hash)
// structure: file, checksum, size
combined_line := str_file + "," + str_hash + "," + str_size
content = append(content, combined_line)
}
var output_file string = target_directory + "/PULP_MANIFEST"
write_lines_to_file(content, output_file)
}
I am testing this using the following command: rm -f test_input/PULP_MANIFEST && go fmt pulp_manifest.go && go build pulp_manifest.go && ./pulp_manifest test_input && cat test_input/PULP_MANIFEST
on Fedora with go 1.20
Known Limitations
- My rewrite does not handle files or directories with "," yet.
Untested
- Files with binary content
- Paths on macOS or Microsoft Windows
- Paths with whitespace
- Symlinks in target_directory
- target_directory as symlink
I am looking for the following feedback:
- bugs and limitations
- a was to add tests: do you have any recommendations for talks or blog posts?
- style & best practice
- a way to use static typing?!
- anything else that you would recommend a novice.
Right now, I believe my rewrite works. Feel free to shatter my assumption. Cheers.
Hi. You have two main ways to achieve this: use an artifact store such as Pulp and https://github.com/pulp/pulp_deb plugin or basically do it yourself by creating the APT repo manually and delivering it over HTTP similar to https://linuxopsys.com/topics/create-your-own-repository-for-packages-on-debian#Step_4_Create_the_Required_Repository_Package_Meta_for_APT
There is no magic. Creating Yum or APT repos boils down to having packages and their metadata in a structured format and making it consumable over HTTP. To get started, you can also inspect any upstream repo in your browser.
Good luck. Feel free to report back if you have more specific questions.