<commit_before>package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	"github.com/go-clix/cli"

	"github.com/grafana/tanka/pkg/jsonnet"
	"github.com/grafana/tanka/pkg/jsonnet/jpath"
)

func toolCmd() *cli.Command {
	cmd := &cli.Command{
		Short: "handy utilities for working with jsonnet",
		Use:   "tool [command]",
	}
	cmd.AddCommand(
		jpathCmd(),
		importsCmd(),
		chartsCmd(),
	)
	return cmd
}

func jpathCmd() *cli.Command {
	cmd := &cli.Command{
		Short: "export JSONNET_PATH for use with other jsonnet tools",
		Use:   "jpath [<file/dir>]",
		Args:  workflowArgs,
	}

	debug := cmd.Flags().BoolP("debug", "d", false, "show debug info")

	cmd.Run = func(cmd *cli.Command, args []string) error {
		path := args[0]

		entrypoint, err := jpath.Entrypoint(path)
		if err != nil {
			return fmt.Errorf("Resolving JPATH: %s", err)
		}

		jsonnetpath, base, root, err := jpath.Resolve(entrypoint)
		if err != nil {
			return fmt.Errorf("Resolving JPATH: %s", err)
		}

		if *debug {
			// log to debug info to stderr
			log.Println("main:", entrypoint)
			log.Println("rootDir:", root)
			log.Println("baseDir:", base)
			log.Println("jpath:", jsonnetpath)
		}

		// print export JSONNET_PATH to stdout
		fmt.Printf("%s", strings.Join(jsonnetpath, ":"))

		return nil
	}

	return cmd
}

func importsCmd() *cli.Command {
	cmd := &cli.Command{
		Use:   "imports <directory>",
		Short: "list all transitive imports of an environment",
		Args:  workflowArgs,
	}

	check := cmd.Flags().StringP("check", "c", "", "git commit hash to check against")

	cmd.Run = func(cmd *cli.Command, args []string) error {
		var modFiles []string
		if *check != "" {
			var err error
			modFiles, err = gitChangedFiles(*check)
			if err != nil {
				return fmt.Errorf("invoking git: %s", err)
			}
		}

		dir, err := filepath.Abs(args[0])
		if err != nil {
			return fmt.Errorf("Loading environment: %s", err)
		}

		fi, err := os.Stat(dir)
		if err != nil {
			return fmt.Errorf("Loading environment: %s", err)
		}

		if !fi.IsDir() {
			return fmt.Errorf("The argument must be an environment's directory, but this does not seem to be the case.")
		}

		deps, err := jsonnet.TransitiveImports(dir)
		if err != nil {
			return fmt.Errorf("Resolving imports: %s", err)
		}

		root, err := gitRoot()
		if err != nil {
			return fmt.Errorf("Invoking git: %s", err)
		}
		if modFiles != nil {
			for _, m := range modFiles {
				mod := filepath.Join(root, m)
				if err != nil {
					return err
				}

				for _, dep := range deps {
					if mod == dep {
						fmt.Printf("Rebuild required. File `%s` imports `%s`, which has been changed in `%s`.\n", args[0], dep, *check)
						os.Exit(16)
					}
				}
			}
			fmt.Printf("Rebuild not required, because no imported files have been changed in `%s`.\n", *check)
			os.Exit(0)
		}

		s, err := json.Marshal(deps)
		if err != nil {
			return fmt.Errorf("Formatting: %s", err)
		}<commit_msg>feat(tool/imports): allow files as environment (#517)<commit_after>package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	"github.com/go-clix/cli"

	"github.com/grafana/tanka/pkg/jsonnet"
	"github.com/grafana/tanka/pkg/jsonnet/jpath"
)

func toolCmd() *cli.Command {
	cmd := &cli.Command{
		Short: "handy utilities for working with jsonnet",
		Use:   "tool [command]",
	}
	cmd.AddCommand(
		jpathCmd(),
		importsCmd(),
		chartsCmd(),
	)
	return cmd
}

func jpathCmd() *cli.Command {
	cmd := &cli.Command{
		Short: "export JSONNET_PATH for use with other jsonnet tools",
		Use:   "jpath [<file/dir>]",
		Args:  workflowArgs,
	}

	debug := cmd.Flags().BoolP("debug", "d", false, "show debug info")

	cmd.Run = func(cmd *cli.Command, args []string) error {
		path := args[0]

		entrypoint, err := jpath.Entrypoint(path)
		if err != nil {
			return fmt.Errorf("Resolving JPATH: %s", err)
		}

		jsonnetpath, base, root, err := jpath.Resolve(entrypoint)
		if err != nil {
			return fmt.Errorf("Resolving JPATH: %s", err)
		}

		if *debug {
			// log to debug info to stderr
			log.Println("main:", entrypoint)
			log.Println("rootDir:", root)
			log.Println("baseDir:", base)
			log.Println("jpath:", jsonnetpath)
		}

		// print export JSONNET_PATH to stdout
		fmt.Printf("%s", strings.Join(jsonnetpath, ":"))

		return nil
	}

	return cmd
}

func importsCmd() *cli.Command {
	cmd := &cli.Command{
		Use:   "imports <path>",
		Short: "list all transitive imports of an environment",
		Args:  workflowArgs,
	}

	check := cmd.Flags().StringP("check", "c", "", "git commit hash to check against")

	cmd.Run = func(cmd *cli.Command, args []string) error {
		var modFiles []string
		if *check != "" {
			var err error
			modFiles, err = gitChangedFiles(*check)
			if err != nil {
				return fmt.Errorf("invoking git: %s", err)
			}
		}

		path, err := filepath.Abs(args[0])
		if err != nil {
			return fmt.Errorf("Loading environment: %s", err)
		}

		deps, err := jsonnet.TransitiveImports(path)
		if err != nil {
			return fmt.Errorf("Resolving imports: %s", err)
		}

		root, err := gitRoot()
		if err != nil {
			return fmt.Errorf("Invoking git: %s", err)
		}
		if modFiles != nil {
			for _, m := range modFiles {
				mod := filepath.Join(root, m)
				if err != nil {
					return err
				}

				for _, dep := range deps {
					if mod == dep {
						fmt.Printf("Rebuild required. File `%s` imports `%s`, which has been changed in `%s`.\n", args[0], dep, *check)
						os.Exit(16)
					}
				}
			}
			fmt.Printf("Rebuild not required, because no imported files have been changed in `%s`.\n", *check)
			os.Exit(0)
		}

		s, err := json.Marshal(deps)
		if err != nil {
			return fmt.Errorf("Formatting: %s", err)
		}
{'fraction_non_alphanumeric': 0.1627988134705985, 'fraction_numerical': 0.002617344268016053, 'mean_word_length': 3.1778425655976674, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5781014', 'n_tokens_mistral': 2379, 'n_tokens_neox': 2087, 'n_words': 569}