Skip to content

Commit

Permalink
ci: change golangci configuration (#180)
Browse files Browse the repository at this point in the history
Changes the golangci configuration by adding a `.golangci.yml` file.
Also fixes lint errors.
  • Loading branch information
MegaRedHand authored Jan 16, 2025
1 parent 1e05bb1 commit 902d7f3
Show file tree
Hide file tree
Showing 14 changed files with 634 additions and 227 deletions.
362 changes: 362 additions & 0 deletions .golangci.yml

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/cmds/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package flags

import "github.com/urfave/cli/v2"

// This is overwritten on release builds
// TODO: move to constants
// This is overwritten on release builds.
// TODO: move to constants.
//
//nolint:gochecknoglobals // this is a constant
var DefaultKurtosisPackage string = ""

//nolint:gochecknoglobals // these are constants
var (
ConfigFilePathFlag = cli.StringFlag{
Name: "config-file",
Expand Down
17 changes: 9 additions & 8 deletions src/cmds/get_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ func GetAddress(ctx *cli.Context) error {

kurtosisCtx, err := kurtosis.InitKurtosisContext()
if err != nil {
return cli.Exit(err, 2)
return cli.Exit(err, 1)
}
enclaveCtx, err := kurtosisCtx.GetEnclaveCtx(ctx.Context, devnetName)
if err != nil {
return cli.Exit(err.Error()+"\n\nFailed to find devnet '"+devnetName+"'. Maybe it's not running?", 3)
return cli.Exit(err.Error()+"\n\nFailed to find devnet '"+devnetName+"'. Maybe it's not running?", 1)
}

err = printAddresses(ctx, args.Slice(), enclaveCtx)

if err != nil {
return cli.Exit(err, 3)
return cli.Exit(err, 1)
}
return nil
}
Expand Down Expand Up @@ -60,7 +60,7 @@ func printAddresses(ctx *cli.Context, args []string, enclaveCtx kurtosis.Enclave
failed = true
readFile = ""
}
file = string(readFile)
file = readFile
cached[artifactName] = file
}
output, ok := readArtifact(file, contractName)
Expand All @@ -79,18 +79,19 @@ func printAddresses(ctx *cli.Context, args []string, enclaveCtx kurtosis.Enclave

func readArtifact(file string, contractName string) (string, bool) {
var jsonPath string
if strings.HasPrefix(contractName, ".") {
switch {
case strings.HasPrefix(contractName, "."):
// This uses the absolute path
jsonPath = "addresses" + contractName + "|@pretty"
} else if contractName != "" {
case contractName != "":
// This searches for `contractName` inside the json
// Since there are multiple results, `|0` is used to get the first one
jsonPath = "@dig:" + contractName + "|0|@pretty"
} else {
default:
// This just prints the whole json
jsonPath = "@pretty"
}
res := gjson.Get(string(file), jsonPath)
res := gjson.Get(file, jsonPath)
if !res.Exists() {
return "", false
}
Expand Down
10 changes: 5 additions & 5 deletions src/cmds/get_ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ func GetPorts(ctx *cli.Context) error {

kurtosisCtx, err := kurtosis.InitKurtosisContext()
if err != nil {
return cli.Exit(err, 2)
return cli.Exit(err, 1)
}
enclaveCtx, err := kurtosisCtx.GetEnclaveCtx(ctx.Context, devnetName)
if err != nil {
return cli.Exit(err.Error()+"\n\nFailed to find devnet '"+devnetName+"'. Maybe it's not running?", 3)
return cli.Exit(err.Error()+"\n\nFailed to find devnet '"+devnetName+"'. Maybe it's not running?", 1)
}
ports, err := getServicePorts(enclaveCtx)
if err != nil {
return cli.Exit(err, 4)
return cli.Exit(err, 1)
}
err = printPorts(ports)
if err != nil {
return cli.Exit(err, 5)
return cli.Exit(err, 1)
}
return nil
}

type ServicePorts map[string]string

// Returns the
// Returns the ports exposed per service.
func getServicePorts(enclaveCtx kurtosis.EnclaveCtx) (map[string]ServicePorts, error) {
servicePorts := make(map[string]ServicePorts)
services, err := enclaveCtx.GetServices()
Expand Down
6 changes: 3 additions & 3 deletions src/cmds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/urfave/cli/v2"
)

// Creates a new devnet configuration with the given context
// Creates a new devnet configuration with the given context.
func InitCmd(ctx *cli.Context) error {
_, configFileName, err := parseArgs(ctx)
if err != nil {
Expand All @@ -18,7 +18,7 @@ func InitCmd(ctx *cli.Context) error {
opts := InitOptions{configFileName}
err = Init(opts)
if err != nil {
return cli.Exit(err, 2)
return cli.Exit(err, 1)
}
fmt.Println("Initialized configuration file:", configFileName)
return nil
Expand All @@ -28,7 +28,7 @@ type InitOptions struct {
ConfigFileName string
}

// Creates a new devnet configuration according to the config
// Creates a new devnet configuration according to the config.
func Init(opts InitOptions) error {
if fileExists(opts.ConfigFileName) {
return errors.New("Config file already exists: " + opts.ConfigFileName)
Expand Down
5 changes: 3 additions & 2 deletions src/cmds/init_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package cmds
package cmds_test

import (
"testing"

"github.com/Layr-Labs/avs-devnet/src/cmds"
"github.com/stretchr/testify/assert"
)

func TestGenerateInitialConfig(t *testing.T) {
tempDir := t.TempDir()
configFile := tempDir + "/test_devnet.yaml"
err := Init(InitOptions{configFile})
err := cmds.Init(cmds.InitOptions{configFile})
assert.NoError(t, err, "Failed to create new config file")
}
114 changes: 66 additions & 48 deletions src/cmds/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/urfave/cli/v2"
)

// Starts the devnet with the given context
// Starts the devnet with the given context.
func StartCmd(ctx *cli.Context) error {
pkgName := flags.KurtosisPackageFlag.Get(ctx)
devnetName, configPath, err := parseArgs(ctx)
Expand All @@ -33,11 +33,11 @@ func StartCmd(ctx *cli.Context) error {
return cli.Exit(err, 1)
}
if !fileExists(configPath) {
return cli.Exit("Config file doesn't exist: "+configPath, 2)
return cli.Exit("Config file doesn't exist: "+configPath, 1)
}
devnetConfig, err := config.LoadFromPath(configPath)
if err != nil {
return cli.Exit(err, 3)
return cli.Exit(err, 1)
}
workingDir := filepath.Dir(configPath)
opts := StartOptions{
Expand All @@ -48,12 +48,12 @@ func StartCmd(ctx *cli.Context) error {
}
err = Start(ctx.Context, opts)
if err != nil {
return cli.Exit(err, 4)
return cli.Exit(err, 1)
}
return nil
}

// Options accepted by Start
// Options accepted by Start.
type StartOptions struct {
// URL of the kurtosis package to run
KurtosisPackageUrl string
Expand All @@ -66,7 +66,7 @@ type StartOptions struct {
DevnetConfig config.DevnetConfig
}

// Starts the devnet with the given context
// Starts the devnet with the given context.
func Start(ctx context.Context, opts StartOptions) error {
kurtosisCtx, err := kurtosis.InitKurtosisContext()
if err != nil {
Expand All @@ -90,7 +90,7 @@ func Start(ctx context.Context, opts StartOptions) error {
return fmt.Errorf("failed when uploading local repos: %w", err)
}

err = uploadStaticFiles(opts.WorkingDir, opts.DevnetConfig, enclaveCtx)
err = uploadStaticFiles(ctx, opts.WorkingDir, opts.DevnetConfig, enclaveCtx)
if err != nil {
return fmt.Errorf("failed when uploading static files: %w", err)
}
Expand Down Expand Up @@ -121,7 +121,7 @@ func Start(ctx context.Context, opts StartOptions) error {
return progress_reporters.ReportProgress(reporter, responseChan)
}

// Uploads the local repositories to the enclave
// Uploads the local repositories to the enclave.
func uploadLocalRepos(dirContext string, config config.DevnetConfig, enclaveCtx *enclaves.EnclaveContext) error {
for _, deployment := range config.Deployments {
if deployment.Repo == "" {
Expand All @@ -146,7 +146,7 @@ func uploadLocalRepos(dirContext string, config config.DevnetConfig, enclaveCtx
// Uploads the script of a single deployment from the repo at the given path to an enclave.
// The deployment script is flattened and uploaded with the deployment name suffixed with '-script'.
// The resulting artifact's structure is similar to the repo's structure, but with only the script and foundry config.
// TODO: to avoid having foundry as a dependency, we should use it via docker
// TODO: to avoid having foundry as a dependency, we should use it via docker.
func uploadLocalRepo(deployment config.Deployment, repoPath string, enclaveCtx *enclaves.EnclaveContext) error {
scriptPath := deployment.GetScriptPath()
scriptOrigin := filepath.Join(repoPath, deployment.ContractsPath, scriptPath)
Expand Down Expand Up @@ -201,16 +201,22 @@ func uploadLocalRepo(deployment config.Deployment, repoPath string, enclaveCtx *
return nil
}

func uploadStaticFiles(dirContext string, config config.DevnetConfig, enclaveCtx *enclaves.EnclaveContext) error {
func uploadStaticFiles(
ctx context.Context,
dirContext string,
config config.DevnetConfig,
enclaveCtx *enclaves.EnclaveContext,
) error {
for artifactName, artifactDetails := range config.Artifacts {
numStaticFiles := 0
numTemplates := 0
for _, fileAttrs := range artifactDetails.Files {
if fileAttrs.StaticFile != nil {
switch {
case fileAttrs.StaticFile != nil:
numStaticFiles += 1
} else if fileAttrs.Template != nil {
case fileAttrs.Template != nil:
numTemplates += 1
} else {
default:
return errors.New("artifact must have either a static file or a template")
}
}
Expand All @@ -231,41 +237,9 @@ func uploadStaticFiles(dirContext string, config config.DevnetConfig, enclaveCtx
for outFileName, fileAttrs := range artifactDetails.Files {
rawUrl := *fileAttrs.StaticFile
destinationFilePath := filepath.Join(outputDir, outFileName)
srcUrl, err := url.Parse(*fileAttrs.StaticFile)
err = uploadStaticFile(ctx, rawUrl, dirContext, destinationFilePath)
if err != nil {
return fmt.Errorf("url '%s' is invalid: %w", rawUrl, err)
}
if isLocalUrl(srcUrl.Scheme) {
// Copy the file to the temp dir
originFilePath := ensureAbs(dirContext, rawUrl)
err = fileCopy(originFilePath, destinationFilePath)
if err != nil {
return fmt.Errorf("failed when copying file: %w", err)
}
} else {
// The file is remote. Download the file
// 1. do GET request
resp, err := http.Get(rawUrl)
if err != nil {
return fmt.Errorf("failed HTTP GET request: %w", err)
}
defer resp.Body.Close()
// 2. check status code
if resp.StatusCode < 200 && resp.StatusCode >= 300 {
return fmt.Errorf("GET request failed with status code: %d", resp.StatusCode)
}

// 3. dump response to file
dstFile, err := os.Create(destinationFilePath)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer dstFile.Close()

_, err = io.Copy(dstFile, resp.Body)
if err != nil {
return fmt.Errorf("failed when downloading file: %w", err)
}
return err
}
}
// Upload temp dir to enclave
Expand All @@ -277,6 +251,50 @@ func uploadStaticFiles(dirContext string, config config.DevnetConfig, enclaveCtx
return nil
}

func uploadStaticFile(ctx context.Context, rawUrl string, dirContext string, destinationFilePath string) error {
srcUrl, err := url.Parse(rawUrl)
if err != nil {
return fmt.Errorf("url '%s' is invalid: %w", rawUrl, err)
}
if isLocalUrl(srcUrl.Scheme) {
// Copy the file to the temp dir
originFilePath := ensureAbs(dirContext, rawUrl)
err = fileCopy(originFilePath, destinationFilePath)
if err != nil {
return fmt.Errorf("failed when copying file: %w", err)
}
return nil
}
// The file is remote. Download the file
// 1. do GET request
req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawUrl, nil)
if err != nil {
return fmt.Errorf("failed to create HTTP request: %w", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("failed HTTP GET request: %w", err)
}
defer resp.Body.Close()
// 2. check status code
if resp.StatusCode < 200 && resp.StatusCode >= 300 {
return fmt.Errorf("GET request failed with status code: %d", resp.StatusCode)
}

// 3. dump response to file
dstFile, err := os.Create(destinationFilePath)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer dstFile.Close()

_, err = io.Copy(dstFile, resp.Body)
if err != nil {
return fmt.Errorf("failed when downloading file: %w", err)
}
return nil
}

// Builds the local docker images for the services in the configuration.
// Starts multiple builds in parallel.
func buildDockerImages(baseDir string, config config.DevnetConfig) error {
Expand All @@ -297,7 +315,7 @@ func buildDockerImages(baseDir string, config config.DevnetConfig) error {
}
}
// Check that all builds were successful and fail if not
errs := make([]error, numBuilds)
errs := make([]error, 0, numBuilds)
for range numBuilds {
errs = append(errs, <-errChan)
}
Expand Down
Loading

0 comments on commit 902d7f3

Please sign in to comment.