Skip to content

Commit

Permalink
new
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinZonda committed Apr 27, 2024
1 parent c1668c6 commit d646ebd
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 126 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Go

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22'

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
22 changes: 0 additions & 22 deletions .github/workflows/rust.yml

This file was deleted.

5 changes: 0 additions & 5 deletions Cargo.lock

This file was deleted.

9 changes: 0 additions & 9 deletions Cargo.toml

This file was deleted.

5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/LGBT-CN/signature-counter

go 1.22.2

require github.com/KevinZonda/GoX v0.0.15 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/KevinZonda/GoX v0.0.15 h1:u+G41/srg8c6bmI6Oqw9iBwYiSQk9FKp9fv/YBZadFk=
github.com/KevinZonda/GoX v0.0.15/go.mod h1:WHV3YUyG+ou3wuUgwO4SRhMiKxLTVK5inajmtR1MUXo=
70 changes: 70 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2024. LGBT-CN & KevinZonda
* This file is part of LGBT-CN Signature Counting.
*/

package main

import (
"fmt"
"github.com/KevinZonda/GoX/pkg/iox"
"github.com/KevinZonda/GoX/pkg/panicx"
"github.com/KevinZonda/GoX/pkg/stringx"
"os"
"strings"
)

const SIGN_BEGIN = "<!-- BEGIN LGBT-CN SIGNATURE -->"
const SIGN_END = "<!-- END LGBT-CN SIGNATURE -->"
const COUNT_BEGIN = "<!-- BEGIN LGBT-CN COUNT -->"
const COUNT_END = "<!-- END LGBT-CN COUNT -->"

func main() {
fileName := os.Args[1]
txt, err := iox.ReadAllText(fileName)
panicx.PanicIfNotNil(err, err)
count, txt := GetSigCount(txt)
txt = SetSigCount(txt, count)
err = iox.WriteAllText(fileName, txt)

}

func Split(content string, beginToken string, endToken string) (string, string, string) {
begins := strings.SplitN(content, beginToken, 2)
token0 := strings.TrimSpace(begins[0])

content = begins[1]

ends := strings.SplitN(content, endToken, 2)
token1 := strings.TrimSpace(ends[0])
token2 := strings.TrimSpace(ends[1])
return token0, token1, token2

}

func GetSigCount(txt string) (int, string) {
header, content, footer := Split(txt, SIGN_BEGIN, SIGN_END)
bodyLines := strings.Split(content, "\n")
bodyLines = stringx.TrimAllAndClean(bodyLines)
content = strings.Join(bodyLines, "\n")
txt = strings.Join([]string{
header,
SIGN_BEGIN,
content,
SIGN_END,
footer,
}, "\n")
return len(bodyLines), txt
}

func SetSigCount(txt string, count int) string {
header, _, footer := Split(txt, COUNT_BEGIN, COUNT_END)
return strings.Join(
[]string{
header,
COUNT_BEGIN,
fmt.Sprintf("已有 %d 人签署!", count),
COUNT_END,
footer,
}, "\n")
}
47 changes: 47 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"github.com/KevinZonda/GoX/pkg/iox"
"os"
"testing"
)

const README = `
BEGIN
<!-- BEGIN LGBT-CN SIGNATURE -->
User1
User2
User5
<!-- END LGBT-CN SIGNATURE -->
<!-- BEGIN LGBT-CN COUNT -->
已有 -1 人签署!
<!-- END LGBT-CN COUNT -->
END
`

const EXPECTED = `BEGIN
<!-- BEGIN LGBT-CN SIGNATURE -->
User1
User2
User5
<!-- END LGBT-CN SIGNATURE -->
<!-- BEGIN LGBT-CN COUNT -->
已有 3 人签署!
<!-- END LGBT-CN COUNT -->
END`

const TEST_FILE = "test.md"

func TestMainFunc(t *testing.T) {
//defer os.Remove(TEST_FILE)
iox.WriteAllText(TEST_FILE, README)
os.Args = []string{"main", TEST_FILE}
main()
txt, _ := iox.ReadAllText(TEST_FILE)
if txt != EXPECTED {
t.Errorf("Expected %s, got %s", EXPECTED, txt)
return
}

}
12 changes: 0 additions & 12 deletions signature-counter.iml

This file was deleted.

78 changes: 0 additions & 78 deletions src/main.rs

This file was deleted.

10 changes: 10 additions & 0 deletions test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
BEGIN
<!-- BEGIN LGBT-CN SIGNATURE -->
User1
User2
User5
<!-- END LGBT-CN SIGNATURE -->
<!-- BEGIN LGBT-CN COUNT -->
已有 3 人签署!
<!-- END LGBT-CN COUNT -->
END

0 comments on commit d646ebd

Please sign in to comment.