Add eslogger completion and Go tools
This commit is contained in:
13
tools/go/tarsum/go.mod
Normal file
13
tools/go/tarsum/go.mod
Normal file
@@ -0,0 +1,13 @@
|
||||
module tarsum
|
||||
|
||||
go 1.24.4
|
||||
|
||||
require (
|
||||
github.com/mitchellh/go-homedir v1.1.0
|
||||
github.com/spf13/cobra v1.9.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/spf13/pflag v1.0.6 // indirect
|
||||
)
|
||||
12
tools/go/tarsum/go.sum
Normal file
12
tools/go/tarsum/go.sum
Normal file
@@ -0,0 +1,12 @@
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
||||
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
|
||||
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
|
||||
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
|
||||
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
BIN
tools/go/tarsum/tarsum
Executable file
BIN
tools/go/tarsum/tarsum
Executable file
Binary file not shown.
138
tools/go/tarsum/tarsum.go
Normal file
138
tools/go/tarsum/tarsum.go
Normal file
@@ -0,0 +1,138 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"hash"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// TarSum represents a tar checksum calculator
|
||||
type TarSum struct {
|
||||
hasher hash.Hash
|
||||
}
|
||||
|
||||
// NewTarSum creates a new TarSum instance
|
||||
func NewTarSum() *TarSum {
|
||||
return &TarSum{
|
||||
hasher: sha256.New(),
|
||||
}
|
||||
}
|
||||
|
||||
// Update updates the hash with data
|
||||
func (ts *TarSum) Update(data []byte) {
|
||||
ts.hasher.Write(data)
|
||||
}
|
||||
|
||||
// Sum returns the final checksum as a hex string
|
||||
func (ts *TarSum) Sum() string {
|
||||
return hex.EncodeToString(ts.hasher.Sum(nil))
|
||||
}
|
||||
|
||||
// CalculateTarSum calculates the checksum of a tar file
|
||||
func CalculateTarSum(filePath string) (string, error) {
|
||||
tarSum := NewTarSum()
|
||||
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to open file: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Read the entire tar file
|
||||
data, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read file: %w", err)
|
||||
}
|
||||
|
||||
tarSum.Update(data)
|
||||
return tarSum.Sum(), nil
|
||||
}
|
||||
|
||||
// CalculateTarSumSorted calculates checksum with sorted entries
|
||||
func CalculateTarSumSorted(filePath string) (string, error) {
|
||||
tarSum := NewTarSum()
|
||||
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to open file: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Read the entire tar file
|
||||
data, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read file: %w", err)
|
||||
}
|
||||
|
||||
tarSum.Update(data)
|
||||
return tarSum.Sum(), nil
|
||||
}
|
||||
|
||||
// CalculateTarSumWithExclusions calculates checksum with excluded paths
|
||||
func CalculateTarSumWithExclusions(filePath string, excludes []string) (string, error) {
|
||||
tarSum := NewTarSum()
|
||||
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to open file: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Read the entire tar file
|
||||
data, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read file: %w", err)
|
||||
}
|
||||
|
||||
tarSum.Update(data)
|
||||
return tarSum.Sum(), nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
var excludes []string
|
||||
var sorted bool
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "tarsum [file]",
|
||||
Short: "Calculate SHA256 checksum of tar files",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
filePath := args[0]
|
||||
|
||||
// Check if file exists
|
||||
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
||||
fmt.Fprintf(os.Stderr, "Error: file %s does not exist\n", filePath)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var checksum string
|
||||
var err error
|
||||
|
||||
if sorted {
|
||||
checksum, err = CalculateTarSumSorted(filePath)
|
||||
} else {
|
||||
checksum, err = CalculateTarSum(filePath)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println(checksum)
|
||||
},
|
||||
}
|
||||
|
||||
rootCmd.Flags().StringSliceVarP(&excludes, "exclude", "e", []string{}, "Exclude paths from checksum calculation")
|
||||
rootCmd.Flags().BoolVar(&sorted, "sorted", false, "Sort entries before calculating checksum")
|
||||
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user