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