Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
WaterLemons2k committed May 29, 2024
0 parents commit 8b1a401
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 2

updates:
- package-ecosystem: github-actions
directory: /
schedule:
interval: daily
- package-ecosystem: gomod
directory: /
schedule:
interval: daily
30 changes: 30 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Release

on:
push:
tags:
- v*

permissions:
contents: write

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod

- name: Release
uses: goreleaser/goreleaser-action@v5
with:
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/autoindex*
index.html

dist/
49 changes: 49 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com

# The lines below are called `modelines`. See `:help modeline`
# Feel free to remove those if you don't want/need to use them.
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj

version: 1

project_name: autoindex

before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy
# you may remove this if you don't need go generate
- go generate ./...

builds:
- main: ./cmd/autoindex
env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin

archives:
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of `uname`.
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
# use zip for windows archives
format_overrides:
- goos: windows
format: zip

changelog:
sort: asc
filters:
exclude:
- "^docs:"
- "^test:"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 WaterLemons2k

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Autoindex CLI

The command-line interface for [autoindex](https://github.com/go-autoindex/autoindex).

## Installation

### GitHub Releases

[Download](https://github.com/go-autoindex/cli/releases)

### `go install`

```sh
go install github.com/go-autoindex/cli/cmd/autoindex@latest
```

## Usage

```sh
$ autoindex
index.html

$ autoindex -h
Usage of ./autoindex:
-dir string
directory to index (default ".")
-dry
dry run
```
35 changes: 35 additions & 0 deletions cmd/autoindex/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"flag"
"fmt"
"os"

"github.com/go-autoindex/autoindex"
"github.com/go-autoindex/cli/internal/path"
)

// flags represents the command line flags
type flags struct {
// dir is the directory to index
dir string
}

var (
dir = flag.String("dir", ".", "directory to index")
dry = flag.Bool("dry", false, "dry run")
)

// parseFlags parses the command line flags.
func parseFlags() *flags {
flag.Parse()

if *dry {
fmt.Fprintln(os.Stderr, "dry run")
autoindex.Opts.Dry = true
}

return &flags{
dir: path.JoinDir(*dir),
}
}
15 changes: 15 additions & 0 deletions cmd/autoindex/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"github.com/go-autoindex/autoindex"
"github.com/go-autoindex/cli/internal/path"
)

func main() {
flags := parseFlags()
autoindex.Opts.Root = flags.dir

if err := path.WalkAndIndex(flags.dir); err != nil {
panic(err)
}
}
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module github.com/go-autoindex/cli

// The minimum version of windows/arm64 support.
go 1.17

require github.com/go-autoindex/autoindex v1.0.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/go-autoindex/autoindex v1.0.1 h1:FEgggK5ujaSGJ/kiG53mG6WFDXs0weu6O/7P43b8bcc=
github.com/go-autoindex/autoindex v1.0.1/go.mod h1:9hV11G2rxfXSlj1cya6tLd9GQhpKp09WZ7LpaHpW11o=
8 changes: 8 additions & 0 deletions internal/path/dir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package path

import "path"

// JoinDir is a wrapper around [path.Join], and adds a trailing slash.
func JoinDir(elems ...string) string {
return path.Join(elems...) + "/"
}
22 changes: 22 additions & 0 deletions internal/path/ignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package path

import (
"strings"

"github.com/go-autoindex/autoindex"
)

// ignored reports whether path should be ignored
func ignored(path string) bool {
return isIndex(path) || isDot(path)
}

// isDot checks if the given path is a dot file.
func isDot(path string) bool {
return strings.HasPrefix(path, ".")
}

// isIndex checks if the given path is "index.html".
func isIndex(path string) bool {
return path == autoindex.Index()
}
45 changes: 45 additions & 0 deletions internal/path/walk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package path

import (
"os"

"github.com/go-autoindex/autoindex"
"github.com/go-autoindex/cli/internal/sort"
)

// WalkAndIndex walks through the directory and generates the index.
func WalkAndIndex(dir string) error {
entries, err := os.ReadDir(dir)
if err != nil {
return err
}
sort.DirFirst(entries)

return autoindex.Gen(walk(dir, entries))
}

// walk walks through the directory and generates info for the index.
func walk(dir string, entries []os.DirEntry) autoindex.Info {
info := autoindex.Info{
Dir: dir,
Entries: make([]string, 0, len(entries)),
}

for _, entry := range entries {
name := entry.Name()
if ignored(name) {
continue
}

if entry.IsDir() {
// Mark name as a directory
name = JoinDir(name)

WalkAndIndex(JoinDir(dir, name))
}

info.Entries = append(info.Entries, name)
}

return info
}
20 changes: 20 additions & 0 deletions internal/sort/sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package sort

import (
"os"
"sort"
"strings"
)

// DirFirst sorts entries by directory first
func DirFirst(entries []os.DirEntry) {
sort.Slice(entries, func(i, j int) bool {
// Directories first
if entries[i].IsDir() != entries[j].IsDir() {
return entries[i].IsDir()
}

// Case insensitive
return strings.ToLower(entries[i].Name()) < strings.ToLower(entries[j].Name())
})
}

0 comments on commit 8b1a401

Please sign in to comment.