Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: switch to go for scripts #5491

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions cliv2/scripts/prepare_licenses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
)

func downloadLicense(url, packageName string) error {
folderPath := filepath.Join(".", "internal", "embedded", "_data", "licenses", packageName)
licenseFileName := filepath.Join(folderPath, "LICENSE")

err := os.MkdirAll(folderPath, 0755)
if err != nil {
return err
}

resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to download   license: %s (status code: %d)", url, resp.StatusCode)
}

licenseFile, err := os.Create(licenseFileName)
if err != nil {
return err
}
defer licenseFile.Close()

_, err = io.Copy(licenseFile, resp.Body)
return err
}

func main() {
pattern := regexp.MustCompile("(?i)COPYING|LICENSE|NOTICE.*")

licenses := []struct {
url string
packageName string
}{
{"https://raw.githubusercontent.com/davecgh/go-spew/master/LICENSE", "github.com/davecgh/go-spew"},
{"https://raw.githubusercontent.com/alexbrainman/sspi/master/LICENSE", "github.com/alexbrainman/sspi"},
{"https://raw.githubusercontent.com/pmezard/go-difflib/master/LICENSE", "github.com/pmezard/go-difflib"},
{"https://go.dev/LICENSE?m=text", "go.dev"},
}

for _, license := range licenses {
err := downloadLicense(license.url, license.packageName)
if err != nil {
fmt.Printf("Error downloading license for %s: %v\n", license.packageName, err)
}
}

licensePath := filepath.Join(".", "internal", "embedded", "_data", "licenses")
err := filepath.Walk(licensePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if !info.IsDir() && !pattern.MatchString(path) {
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
return err
}
} else {
fmt.Println(path)
}
return nil
})

if err != nil {
fmt.Println("Error cleaning up licenses:", err)
}
}
46 changes: 0 additions & 46 deletions cliv2/scripts/prepare_licenses.py

This file was deleted.

7 changes: 1 addition & 6 deletions cliv2/scripts/prepare_licenses.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#!/usr/bin/env bash
set -euo pipefail
BASEDIR=$(dirname "$0")
PYTHON_VERSION=""

if python3 -c 'print("python3")' > /dev/null 2>&1; then
PYTHON_VERSION="3"
fi

python$PYTHON_VERSION $BASEDIR/prepare_licenses.py
go run $BASEDIR/prepare_licenses.go
Loading