~seirdy/moac

70fe143f8d0836700927954a3ce4ec9e5d57089a — Rohan Kumar 6 months ago 031b544
Refactor: move grapheme logic to shared file

Forgot to format the grandparent commit; clean up import-order,
whitespace, etc. The complexity of main1() grew a bit and the grapheme
handling was repetitive, so moved it to the shared internal/cli subpkg.
5 files changed, 25 insertions(+), 19 deletions(-)

M .go-arch-lint.yml
M .golangci.yml
M cmd/moac-pwgen/main.go
M cmd/moac/main.go
M internal/cli/cli.go
M .go-arch-lint.yml => .go-arch-lint.yml +4 -2
@@ 46,7 46,6 @@ deps:
  pwgen-cli:
    canUse:
      - getopt
      - uniseg
    mayDependOn:
      - moac
      - cliShared


@@ 57,7 56,6 @@ deps:
    canUse:
      - getopt
      - term
      - uniseg
    mayDependOn:
      - moac
      - cliShared


@@ 75,3 73,7 @@ deps:
      - charsets
      - entropy
      - bounds
  cliShared:
    canUse:
      - uniseg


M .golangci.yml => .golangci.yml +1 -1
@@ 184,7 184,7 @@ linters-settings:
    q: true
  funlen:
    lines: 80
    statements: 23
    statements: 25
  revive:
    confidence: 0.21
    rules:

M cmd/moac-pwgen/main.go => cmd/moac-pwgen/main.go +2 -7
@@ 12,7 12,6 @@ import (
	"git.sr.ht/~seirdy/moac/v2/internal/sanitize"
	"git.sr.ht/~seirdy/moac/v2/pwgen"
	"git.sr.ht/~sircmpwn/getopt"
	"github.com/rivo/uniseg"
)

const (


@@ 153,12 152,8 @@ func main1() int {
		return 1
	}

	graphemes := uniseg.NewGraphemes(strings.Join(args, ""))
	for graphemes.Next() {
		if len(graphemes.Runes()) > 1 {
			fmt.Fprintf(os.Stderr, "warning: charsets contain grapheme clusters, will be treated as distinct codepoints\n")
			break
		}
	if cli.HasGrapheme(strings.Join(args, "")) {
		fmt.Fprintf(os.Stderr, "warning: charsets contain grapheme clusters, will be treated as distinct codepoints\n")
	}

	pwr.CharsetsWanted = charsets.ParseCharsets(setCharsetNames(args))

M cmd/moac/main.go => cmd/moac/main.go +4 -9
@@ 12,7 12,6 @@ import (
	"git.sr.ht/~seirdy/moac/v2/internal/cli"
	"git.sr.ht/~sircmpwn/getopt"
	"golang.org/x/term"
	"github.com/rivo/uniseg"
)

const (


@@ 160,14 159,6 @@ func getOutput() (output float64, exitEarly bool, err error) {
		return output, exitEarly, fmt.Errorf("moac: %w", err)
	}

	graphemes := uniseg.NewGraphemes(givens.Password)
	for graphemes.Next() {
		if len(graphemes.Runes()) > 1 {
			fmt.Fprintf(os.Stderr, "warning: charsets contain grapheme clusters, will be treated as distinct codepoints\n")
			break
		}
	}

	cmd := strengthCmd

	if len(os.Args) > optind {


@@ 196,6 187,10 @@ func processPassword(oldPw string) (newPw string, err error) {
		newPw = newPw[:len(newPw)-1]
	}

	if cli.HasGrapheme(newPw) {
		fmt.Fprintf(os.Stderr, "warning: charsets contain grapheme clusters, will be treated as distinct codepoints\n")
	}

	return newPw, err
}


M internal/cli/cli.go => internal/cli/cli.go +14 -0
@@ 6,6 6,8 @@ import (
	"fmt"
	"os"
	"runtime/debug"

	"github.com/rivo/uniseg"
)

// FloatFmt defines how many digits of a float to print.


@@ 41,3 43,15 @@ func GetVersion() string {

	return version
}

// HasGrapheme returns true if a string contains any grapheme clusters, false otherwise.
func HasGrapheme(str string) bool {
	graphemes := uniseg.NewGraphemes(str)
	for graphemes.Next() {
		if len(graphemes.Runes()) > 1 {
			return true
		}
	}

	return false
}