Skip to content

Commit 8b1a401

Browse files
committed
Initial commit
0 parents  commit 8b1a401

File tree

14 files changed

+297
-0
lines changed

14 files changed

+297
-0
lines changed

.github/dependabot.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: 2
2+
3+
updates:
4+
- package-ecosystem: github-actions
5+
directory: /
6+
schedule:
7+
interval: daily
8+
- package-ecosystem: gomod
9+
directory: /
10+
schedule:
11+
interval: daily

.github/workflows/release.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- v*
7+
8+
permissions:
9+
contents: write
10+
11+
jobs:
12+
release:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v4
17+
with:
18+
fetch-depth: 0
19+
20+
- name: Setup Go
21+
uses: actions/setup-go@v5
22+
with:
23+
go-version-file: go.mod
24+
25+
- name: Release
26+
uses: goreleaser/goreleaser-action@v5
27+
with:
28+
args: release --clean
29+
env:
30+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/autoindex*
2+
index.html
3+
4+
dist/

.goreleaser.yml

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# This is an example .goreleaser.yml file with some sensible defaults.
2+
# Make sure to check the documentation at https://goreleaser.com
3+
4+
# The lines below are called `modelines`. See `:help modeline`
5+
# Feel free to remove those if you don't want/need to use them.
6+
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
7+
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
8+
9+
version: 1
10+
11+
project_name: autoindex
12+
13+
before:
14+
hooks:
15+
# You may remove this if you don't use go modules.
16+
- go mod tidy
17+
# you may remove this if you don't need go generate
18+
- go generate ./...
19+
20+
builds:
21+
- main: ./cmd/autoindex
22+
env:
23+
- CGO_ENABLED=0
24+
goos:
25+
- linux
26+
- windows
27+
- darwin
28+
29+
archives:
30+
- format: tar.gz
31+
# this name template makes the OS and Arch compatible with the results of `uname`.
32+
name_template: >-
33+
{{ .ProjectName }}_
34+
{{- title .Os }}_
35+
{{- if eq .Arch "amd64" }}x86_64
36+
{{- else if eq .Arch "386" }}i386
37+
{{- else }}{{ .Arch }}{{ end }}
38+
{{- if .Arm }}v{{ .Arm }}{{ end }}
39+
# use zip for windows archives
40+
format_overrides:
41+
- goos: windows
42+
format: zip
43+
44+
changelog:
45+
sort: asc
46+
filters:
47+
exclude:
48+
- "^docs:"
49+
- "^test:"

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 WaterLemons2k
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Autoindex CLI
2+
3+
The command-line interface for [autoindex](https://github.com/go-autoindex/autoindex).
4+
5+
## Installation
6+
7+
### GitHub Releases
8+
9+
[Download](https://github.com/go-autoindex/cli/releases)
10+
11+
### `go install`
12+
13+
```sh
14+
go install github.com/go-autoindex/cli/cmd/autoindex@latest
15+
```
16+
17+
## Usage
18+
19+
```sh
20+
$ autoindex
21+
index.html
22+
23+
$ autoindex -h
24+
Usage of ./autoindex:
25+
-dir string
26+
directory to index (default ".")
27+
-dry
28+
dry run
29+
```

cmd/autoindex/flags.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"os"
7+
8+
"github.com/go-autoindex/autoindex"
9+
"github.com/go-autoindex/cli/internal/path"
10+
)
11+
12+
// flags represents the command line flags
13+
type flags struct {
14+
// dir is the directory to index
15+
dir string
16+
}
17+
18+
var (
19+
dir = flag.String("dir", ".", "directory to index")
20+
dry = flag.Bool("dry", false, "dry run")
21+
)
22+
23+
// parseFlags parses the command line flags.
24+
func parseFlags() *flags {
25+
flag.Parse()
26+
27+
if *dry {
28+
fmt.Fprintln(os.Stderr, "dry run")
29+
autoindex.Opts.Dry = true
30+
}
31+
32+
return &flags{
33+
dir: path.JoinDir(*dir),
34+
}
35+
}

cmd/autoindex/main.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"github.com/go-autoindex/autoindex"
5+
"github.com/go-autoindex/cli/internal/path"
6+
)
7+
8+
func main() {
9+
flags := parseFlags()
10+
autoindex.Opts.Root = flags.dir
11+
12+
if err := path.WalkAndIndex(flags.dir); err != nil {
13+
panic(err)
14+
}
15+
}

go.mod

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module github.com/go-autoindex/cli
2+
3+
// The minimum version of windows/arm64 support.
4+
go 1.17
5+
6+
require github.com/go-autoindex/autoindex v1.0.1

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/go-autoindex/autoindex v1.0.1 h1:FEgggK5ujaSGJ/kiG53mG6WFDXs0weu6O/7P43b8bcc=
2+
github.com/go-autoindex/autoindex v1.0.1/go.mod h1:9hV11G2rxfXSlj1cya6tLd9GQhpKp09WZ7LpaHpW11o=

internal/path/dir.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package path
2+
3+
import "path"
4+
5+
// JoinDir is a wrapper around [path.Join], and adds a trailing slash.
6+
func JoinDir(elems ...string) string {
7+
return path.Join(elems...) + "/"
8+
}

internal/path/ignore.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package path
2+
3+
import (
4+
"strings"
5+
6+
"github.com/go-autoindex/autoindex"
7+
)
8+
9+
// ignored reports whether path should be ignored
10+
func ignored(path string) bool {
11+
return isIndex(path) || isDot(path)
12+
}
13+
14+
// isDot checks if the given path is a dot file.
15+
func isDot(path string) bool {
16+
return strings.HasPrefix(path, ".")
17+
}
18+
19+
// isIndex checks if the given path is "index.html".
20+
func isIndex(path string) bool {
21+
return path == autoindex.Index()
22+
}

internal/path/walk.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package path
2+
3+
import (
4+
"os"
5+
6+
"github.com/go-autoindex/autoindex"
7+
"github.com/go-autoindex/cli/internal/sort"
8+
)
9+
10+
// WalkAndIndex walks through the directory and generates the index.
11+
func WalkAndIndex(dir string) error {
12+
entries, err := os.ReadDir(dir)
13+
if err != nil {
14+
return err
15+
}
16+
sort.DirFirst(entries)
17+
18+
return autoindex.Gen(walk(dir, entries))
19+
}
20+
21+
// walk walks through the directory and generates info for the index.
22+
func walk(dir string, entries []os.DirEntry) autoindex.Info {
23+
info := autoindex.Info{
24+
Dir: dir,
25+
Entries: make([]string, 0, len(entries)),
26+
}
27+
28+
for _, entry := range entries {
29+
name := entry.Name()
30+
if ignored(name) {
31+
continue
32+
}
33+
34+
if entry.IsDir() {
35+
// Mark name as a directory
36+
name = JoinDir(name)
37+
38+
WalkAndIndex(JoinDir(dir, name))
39+
}
40+
41+
info.Entries = append(info.Entries, name)
42+
}
43+
44+
return info
45+
}

internal/sort/sort.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package sort
2+
3+
import (
4+
"os"
5+
"sort"
6+
"strings"
7+
)
8+
9+
// DirFirst sorts entries by directory first
10+
func DirFirst(entries []os.DirEntry) {
11+
sort.Slice(entries, func(i, j int) bool {
12+
// Directories first
13+
if entries[i].IsDir() != entries[j].IsDir() {
14+
return entries[i].IsDir()
15+
}
16+
17+
// Case insensitive
18+
return strings.ToLower(entries[i].Name()) < strings.ToLower(entries[j].Name())
19+
})
20+
}

0 commit comments

Comments
 (0)