refactor: modern code using go fix
This commit is contained in:
@@ -113,15 +113,15 @@ func truncate(in string, max int) string {
|
|||||||
return in[:max-3] + "..."
|
return in[:max-3] + "..."
|
||||||
}
|
}
|
||||||
|
|
||||||
func outputJSON(v interface{}) error {
|
func outputJSON(v any) error {
|
||||||
encoder := json.NewEncoder(os.Stdout)
|
encoder := json.NewEncoder(os.Stdout)
|
||||||
encoder.SetIndent("", " ")
|
encoder.SetIndent("", " ")
|
||||||
return encoder.Encode(v)
|
return encoder.Encode(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func outputWarning(message string, args ...interface{}) {
|
func outputWarning(message string, args ...any) {
|
||||||
if IsJSONOutput() {
|
if IsJSONOutput() {
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"warning": fmt.Sprintf(message, args...),
|
"warning": fmt.Sprintf(message, args...),
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -236,7 +236,7 @@ func TestSetJSONOutput(t *testing.T) {
|
|||||||
done := make(chan bool)
|
done := make(chan bool)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for i := 0; i < 100; i++ {
|
for range 100 {
|
||||||
SetJSONOutput(true)
|
SetJSONOutput(true)
|
||||||
_ = IsJSONOutput()
|
_ = IsJSONOutput()
|
||||||
SetJSONOutput(false)
|
SetJSONOutput(false)
|
||||||
@@ -245,7 +245,7 @@ func TestSetJSONOutput(t *testing.T) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for i := 0; i < 100; i++ {
|
for range 100 {
|
||||||
_ = IsJSONOutput()
|
_ = IsJSONOutput()
|
||||||
}
|
}
|
||||||
done <- true
|
done <- true
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ func runStatusCommand(cfg *config.Config) error {
|
|||||||
|
|
||||||
if !isDaemonRunning(pidFile) {
|
if !isDaemonRunning(pidFile) {
|
||||||
if IsJSONOutput() {
|
if IsJSONOutput() {
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"status": "not_running",
|
"status": "not_running",
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
@@ -99,7 +99,7 @@ func runStatusCommand(cfg *config.Config) error {
|
|||||||
data, err := os.ReadFile(pidFile)
|
data, err := os.ReadFile(pidFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if IsJSONOutput() {
|
if IsJSONOutput() {
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"status": "running",
|
"status": "running",
|
||||||
"error": fmt.Sprintf("PID file exists but cannot be read: %v", err),
|
"error": fmt.Sprintf("PID file exists but cannot be read: %v", err),
|
||||||
})
|
})
|
||||||
@@ -112,7 +112,7 @@ func runStatusCommand(cfg *config.Config) error {
|
|||||||
pid, err := strconv.Atoi(string(data))
|
pid, err := strconv.Atoi(string(data))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if IsJSONOutput() {
|
if IsJSONOutput() {
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"status": "running",
|
"status": "running",
|
||||||
"error": fmt.Sprintf("PID file exists but contains invalid PID: %v", err),
|
"error": fmt.Sprintf("PID file exists but contains invalid PID: %v", err),
|
||||||
})
|
})
|
||||||
@@ -123,7 +123,7 @@ func runStatusCommand(cfg *config.Config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if IsJSONOutput() {
|
if IsJSONOutput() {
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"status": "running",
|
"status": "running",
|
||||||
"pid": pid,
|
"pid": pid,
|
||||||
})
|
})
|
||||||
@@ -171,7 +171,7 @@ func stopDaemon(cfg *config.Config) error {
|
|||||||
_ = os.Remove(pidFile)
|
_ = os.Remove(pidFile)
|
||||||
|
|
||||||
if IsJSONOutput() {
|
if IsJSONOutput() {
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"action": "stopped",
|
"action": "stopped",
|
||||||
"pid": pid,
|
"pid": pid,
|
||||||
})
|
})
|
||||||
@@ -219,7 +219,7 @@ func runDaemon(cfg *config.Config) error {
|
|||||||
return fmt.Errorf("cannot write PID file: %w", err)
|
return fmt.Errorf("cannot write PID file: %w", err)
|
||||||
}
|
}
|
||||||
if IsJSONOutput() {
|
if IsJSONOutput() {
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"action": "started",
|
"action": "started",
|
||||||
"pid": pid,
|
"pid": pid,
|
||||||
"pid_file": pidFile,
|
"pid_file": pidFile,
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ func postDelete(repo repositories.PostRepository, args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if IsJSONOutput() {
|
if IsJSONOutput() {
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"action": "post_deleted",
|
"action": "post_deleted",
|
||||||
"id": id,
|
"id": id,
|
||||||
})
|
})
|
||||||
@@ -158,7 +158,7 @@ func postList(postQueries *services.PostQueries, args []string) error {
|
|||||||
CreatedAt: p.CreatedAt.Format("2006-01-02 15:04:05"),
|
CreatedAt: p.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"posts": postsJSON,
|
"posts": postsJSON,
|
||||||
"count": len(postsJSON),
|
"count": len(postsJSON),
|
||||||
})
|
})
|
||||||
@@ -298,7 +298,7 @@ func postSearch(postQueries *services.PostQueries, args []string) error {
|
|||||||
CreatedAt: p.CreatedAt.Format("2006-01-02 15:04:05"),
|
CreatedAt: p.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"search_term": sanitizedTerm,
|
"search_term": sanitizedTerm,
|
||||||
"posts": postsJSON,
|
"posts": postsJSON,
|
||||||
"count": len(postsJSON),
|
"count": len(postsJSON),
|
||||||
|
|||||||
@@ -508,9 +508,9 @@ func TestProgressIndicator_Concurrency(t *testing.T) {
|
|||||||
pi := NewProgressIndicator(100, "Concurrent test")
|
pi := NewProgressIndicator(100, "Concurrent test")
|
||||||
done := make(chan bool)
|
done := make(chan bool)
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
for range 10 {
|
||||||
go func() {
|
go func() {
|
||||||
for j := 0; j < 10; j++ {
|
for range 10 {
|
||||||
pi.Increment()
|
pi.Increment()
|
||||||
time.Sleep(1 * time.Millisecond)
|
time.Sleep(1 * time.Millisecond)
|
||||||
}
|
}
|
||||||
@@ -518,7 +518,7 @@ func TestProgressIndicator_Concurrency(t *testing.T) {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
for range 10 {
|
||||||
<-done
|
<-done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ func prunePosts(postRepo repositories.PostRepository, args []string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if *dryRun {
|
if *dryRun {
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"action": "prune_posts",
|
"action": "prune_posts",
|
||||||
"dry_run": true,
|
"dry_run": true,
|
||||||
"posts": postsJSON,
|
"posts": postsJSON,
|
||||||
@@ -110,7 +110,7 @@ func prunePosts(postRepo repositories.PostRepository, args []string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("hard delete posts: %w", err)
|
return fmt.Errorf("hard delete posts: %w", err)
|
||||||
}
|
}
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"action": "prune_posts",
|
"action": "prune_posts",
|
||||||
"deleted_count": deletedCount,
|
"deleted_count": deletedCount,
|
||||||
})
|
})
|
||||||
@@ -178,7 +178,7 @@ func pruneUsers(userRepo repositories.UserRepository, postRepo repositories.Post
|
|||||||
userCount := len(users)
|
userCount := len(users)
|
||||||
if userCount == 0 {
|
if userCount == 0 {
|
||||||
if IsJSONOutput() {
|
if IsJSONOutput() {
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"action": "prune_users",
|
"action": "prune_users",
|
||||||
"count": 0,
|
"count": 0,
|
||||||
})
|
})
|
||||||
@@ -211,7 +211,7 @@ func pruneUsers(userRepo repositories.UserRepository, postRepo repositories.Post
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if *dryRun {
|
if *dryRun {
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"action": "prune_users",
|
"action": "prune_users",
|
||||||
"dry_run": true,
|
"dry_run": true,
|
||||||
"users": usersJSON,
|
"users": usersJSON,
|
||||||
@@ -229,7 +229,7 @@ func pruneUsers(userRepo repositories.UserRepository, postRepo repositories.Post
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("hard delete all users and posts: %w", err)
|
return fmt.Errorf("hard delete all users and posts: %w", err)
|
||||||
}
|
}
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"action": "prune_users",
|
"action": "prune_users",
|
||||||
"deleted_count": totalDeleted,
|
"deleted_count": totalDeleted,
|
||||||
"with_posts": true,
|
"with_posts": true,
|
||||||
@@ -242,7 +242,7 @@ func pruneUsers(userRepo repositories.UserRepository, postRepo repositories.Post
|
|||||||
}
|
}
|
||||||
deletedCount++
|
deletedCount++
|
||||||
}
|
}
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"action": "prune_users",
|
"action": "prune_users",
|
||||||
"deleted_count": deletedCount,
|
"deleted_count": deletedCount,
|
||||||
"with_posts": false,
|
"with_posts": false,
|
||||||
@@ -328,7 +328,7 @@ func pruneAll(userRepo repositories.UserRepository, postRepo repositories.PostRe
|
|||||||
|
|
||||||
if IsJSONOutput() {
|
if IsJSONOutput() {
|
||||||
if *dryRun {
|
if *dryRun {
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"action": "prune_all",
|
"action": "prune_all",
|
||||||
"dry_run": true,
|
"dry_run": true,
|
||||||
"user_count": len(userCount),
|
"user_count": len(userCount),
|
||||||
@@ -343,7 +343,7 @@ func pruneAll(userRepo repositories.UserRepository, postRepo repositories.PostRe
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("hard delete all: %w", err)
|
return fmt.Errorf("hard delete all: %w", err)
|
||||||
}
|
}
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"action": "prune_all",
|
"action": "prune_all",
|
||||||
"deleted_count": totalDeleted,
|
"deleted_count": totalDeleted,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -321,7 +321,7 @@ func createUsers(g *seedGenerator, userRepo repositories.UserRepository, count i
|
|||||||
}
|
}
|
||||||
progress := maybeProgress(count, desc)
|
progress := maybeProgress(count, desc)
|
||||||
users := make([]database.User, 0, count)
|
users := make([]database.User, 0, count)
|
||||||
for i := 0; i < count; i++ {
|
for i := range count {
|
||||||
user, err := g.createSingleUser(userRepo, i+1)
|
user, err := g.createSingleUser(userRepo, i+1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("create random user: %w", err)
|
return nil, fmt.Errorf("create random user: %w", err)
|
||||||
@@ -343,7 +343,7 @@ func createPosts(g *seedGenerator, postRepo repositories.PostRepository, authorI
|
|||||||
}
|
}
|
||||||
progress := maybeProgress(count, desc)
|
progress := maybeProgress(count, desc)
|
||||||
posts := make([]database.Post, 0, count)
|
posts := make([]database.Post, 0, count)
|
||||||
for i := 0; i < count; i++ {
|
for i := range count {
|
||||||
post, err := g.createSinglePost(postRepo, authorID, i+1)
|
post, err := g.createSinglePost(postRepo, authorID, i+1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("create random post: %w", err)
|
return nil, fmt.Errorf("create random post: %w", err)
|
||||||
|
|||||||
@@ -169,7 +169,7 @@ func userCreate(cfg *config.Config, repo repositories.UserRepository, args []str
|
|||||||
}
|
}
|
||||||
|
|
||||||
if IsJSONOutput() {
|
if IsJSONOutput() {
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"action": "user_created",
|
"action": "user_created",
|
||||||
"id": user.ID,
|
"id": user.ID,
|
||||||
"username": user.Username,
|
"username": user.Username,
|
||||||
@@ -296,7 +296,7 @@ func userUpdate(cfg *config.Config, repo repositories.UserRepository, refreshTok
|
|||||||
}
|
}
|
||||||
|
|
||||||
if IsJSONOutput() {
|
if IsJSONOutput() {
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"action": "user_updated",
|
"action": "user_updated",
|
||||||
"id": user.ID,
|
"id": user.ID,
|
||||||
"username": user.Username,
|
"username": user.Username,
|
||||||
@@ -424,7 +424,7 @@ func userDelete(cfg *config.Config, repo repositories.UserRepository, args []str
|
|||||||
}
|
}
|
||||||
|
|
||||||
if IsJSONOutput() {
|
if IsJSONOutput() {
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"action": "user_deleted",
|
"action": "user_deleted",
|
||||||
"id": id,
|
"id": id,
|
||||||
"username": user.Username,
|
"username": user.Username,
|
||||||
@@ -479,7 +479,7 @@ func userList(repo repositories.UserRepository, args []string) error {
|
|||||||
CreatedAt: u.CreatedAt.Format("2006-01-02 15:04:05"),
|
CreatedAt: u.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"users": usersJSON,
|
"users": usersJSON,
|
||||||
"count": len(usersJSON),
|
"count": len(usersJSON),
|
||||||
})
|
})
|
||||||
@@ -578,7 +578,7 @@ func userLock(cfg *config.Config, repo repositories.UserRepository, args []strin
|
|||||||
|
|
||||||
if user.Locked {
|
if user.Locked {
|
||||||
if IsJSONOutput() {
|
if IsJSONOutput() {
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"action": "user_lock",
|
"action": "user_lock",
|
||||||
"id": id,
|
"id": id,
|
||||||
"username": user.Username,
|
"username": user.Username,
|
||||||
@@ -604,7 +604,7 @@ func userLock(cfg *config.Config, repo repositories.UserRepository, args []strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
if IsJSONOutput() {
|
if IsJSONOutput() {
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"action": "user_locked",
|
"action": "user_locked",
|
||||||
"id": id,
|
"id": id,
|
||||||
"username": user.Username,
|
"username": user.Username,
|
||||||
@@ -653,7 +653,7 @@ func userUnlock(cfg *config.Config, repo repositories.UserRepository, args []str
|
|||||||
|
|
||||||
if !user.Locked {
|
if !user.Locked {
|
||||||
if IsJSONOutput() {
|
if IsJSONOutput() {
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"action": "user_unlock",
|
"action": "user_unlock",
|
||||||
"id": id,
|
"id": id,
|
||||||
"username": user.Username,
|
"username": user.Username,
|
||||||
@@ -679,7 +679,7 @@ func userUnlock(cfg *config.Config, repo repositories.UserRepository, args []str
|
|||||||
}
|
}
|
||||||
|
|
||||||
if IsJSONOutput() {
|
if IsJSONOutput() {
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"action": "user_unlocked",
|
"action": "user_unlocked",
|
||||||
"id": id,
|
"id": id,
|
||||||
"username": user.Username,
|
"username": user.Username,
|
||||||
@@ -732,7 +732,7 @@ func resetUserPassword(cfg *config.Config, repo repositories.UserRepository, ses
|
|||||||
}
|
}
|
||||||
|
|
||||||
if IsJSONOutput() {
|
if IsJSONOutput() {
|
||||||
outputJSON(map[string]interface{}{
|
outputJSON(map[string]any{
|
||||||
"action": "password_reset",
|
"action": "password_reset",
|
||||||
"id": userID,
|
"id": userID,
|
||||||
"username": user.Username,
|
"username": user.Username,
|
||||||
|
|||||||
Reference in New Issue
Block a user