Skip to content

Commit

Permalink
Merge pull request #7 from mistweaverco/feat/allow-filenames
Browse files Browse the repository at this point in the history
feat: allow passing filenames
  • Loading branch information
gorillamoe authored Jul 24, 2024
2 parents ff2403a + 16ef59f commit b8b569e
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 47 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,60 @@ Format all `.http` and `.rest` files in the current directory and its subdirecto
kulala-fmt
```

Format specific `.http` and `.rest` files.

```sh
kulala-fmt file1.http file2.rest http/*.http
```

Format all `.http` and `.rest` files in the current directory and its subdirectories and
prints the written output to the console:

```sh
kulala-fmt --verbose
```
Format specific `.http` and `.rest` files and
prints the written output to the console:

```sh
kulala-fmt --verbose file1.http file2.rest http/*.http
```

Check if all `.http` and `.rest` files in the current directory and its subdirectories are formatted:

```sh
kulala-fmt --check
```

Check if specific `.http` and `.rest` files are formatted:

```sh
kulala-fmt --check file1.http file2.rest http/*.http
```

Check if all `.http` and `.rest` files in the current directory and
its subdirectories are formatted and
prints the desired output to the console:

```sh
kulala-fmt --check --verbose
```

Check if specific `.http` and `.rest` files are formatted and
prints the desired output to the console:

```sh
kulala-fmt --check --verbose file1.http file2.rest http/*.http
```

## What does it do?

- Checks if the file is formatted and valid
- Removes extraneous newlines
- Makes sure document variables are at the top of the file
- Lowercases all headers
- Puts all metadata right after the headers
- Ensures all comments are using `#` and not `//`

If run on all files it also warns when it finds both `.env` and `http-client.env.json`
files in the same directory, because that might cause unexpected behavior.
19 changes: 0 additions & 19 deletions cmd/kulalafmt/about.go

This file was deleted.

16 changes: 13 additions & 3 deletions cmd/kulalafmt/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,30 @@ package kulalafmt

import (
"os"
"runtime"

"github.com/charmbracelet/log"
"github.com/mistweaverco/kulala-fmt/internal/config"
"github.com/mistweaverco/kulala-fmt/internal/parser"
"github.com/spf13/cobra"
)

var VERSION string
var cfg = config.NewConfig(config.Config{})

var rootCmd = &cobra.Command{
Use: "kulala-fmt",
Short: "An opinionated 🦄 .http and .rest 🐼 files linter 💄 and formatter ⚡.",
Long: "Formats and lints .http and .rest files in the current directory and subdirectories.",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
parser.Start(cfg.GetConfigFlags())
Run: func(cmd *cobra.Command, files []string) {
if cfg.Flags.Version {
log.Info("Version", runtime.GOOS, VERSION)
return
}
if len(files) > 0 {
parser.Start(cfg.GetConfigFlags(), files)
} else {
parser.StartAllFiles(cfg.GetConfigFlags())
}
},
}
Expand All @@ -31,4 +40,5 @@ func Execute() {
func init() {
rootCmd.PersistentFlags().BoolVar(&cfg.Flags.Check, "check", false, "check")
rootCmd.PersistentFlags().BoolVar(&cfg.Flags.Verbose, "verbose", false, "verbose")
rootCmd.PersistentFlags().BoolVar(&cfg.Flags.Version, "version", false, "version")
}
22 changes: 0 additions & 22 deletions cmd/kulalafmt/version.go

This file was deleted.

1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
type ConfigFlags struct {
Check bool
Verbose bool
Version bool
}

type Config struct {
Expand Down
8 changes: 6 additions & 2 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func parseSection(section string, document *Document) Section {
return parsedSection
}

func validateSection(section Section, filepath string, document Document) {
func validateSection(section Section, filepath string, document *Document) {
if section.Method == "" {
log.Error("Section is missing method", "file", filepath)
document.Valid = false
Expand Down Expand Up @@ -138,6 +138,10 @@ func documentToString(document Document) string {
}

func parser(filepath string, flags config.ConfigFlags) {
if !strings.HasSuffix(filepath, ".http") && !strings.HasSuffix(filepath, ".rest") {
log.Warn("File is not a .http or .rest file, skipping.", "file", filepath)
return
}
document := Document{
Valid: true,
Variables: []string{},
Expand All @@ -155,7 +159,7 @@ func parser(filepath string, flags config.ConfigFlags) {
}
parsedSection := parseSection(section, &document)
document.Sections = append(document.Sections, parsedSection)
validateSection(parsedSection, filepath, document)
validateSection(parsedSection, filepath, &document)
}
if !document.Valid {
log.Error("File is not valid, can't fix, skipping.", "file", filepath)
Expand Down
8 changes: 7 additions & 1 deletion internal/parser/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ func isHttpClientEnvJson(filepath string) bool {
return strings.HasSuffix(filepath, "http-client.env.json")
}

func Start(flags config.ConfigFlags) {
func Start(flags config.ConfigFlags, files []string) {
for _, filepath := range files {
parser(filepath, flags)
}
}

func StartAllFiles(flags config.ConfigFlags) {
filepath_pkgs, _ := filewalker.GetFiles()
for _, filepath_pkg := range filepath_pkgs {
dirpath := getDirectoryPath(filepath_pkg[0])
Expand Down

0 comments on commit b8b569e

Please sign in to comment.