Browse Source
refactor: streamline configuration management and enhance UI interactions
refactor: streamline configuration management and enhance UI interactions
- Updated go.mod and go.sum to include necessary dependencies. - Refactored README.md for clearer installation instructions and improved formatting. - Enhanced main.go with better error handling and user feedback during execution. - Improved configuration management in config.go, ensuring atomic writes and better error handling. - Updated language support in lang.go for clearer user messages. - Enhanced process management in manager.go to ensure more reliable process termination. - Improved UI display methods for better user experience. - Removed outdated test file generator_test.go to clean up the codebase. - Updated install.ps1 script for better output formatting and error handling.pull/85/head
13 changed files with 523 additions and 484 deletions
-
34README.md
-
308cmd/cursor-id-modifier/main.go
-
5go.mod
-
1go.sum
-
45internal/config/config.go
-
129internal/lang/lang.go
-
148internal/process/manager.go
-
105internal/ui/display.go
-
14internal/ui/logo.go
-
38internal/ui/spinner.go
-
106pkg/idgen/generator.go
-
26pkg/idgen/generator_test.go
-
48scripts/install.ps1
@ -1,95 +1,59 @@ |
|||
package idgen |
|||
|
|||
import ( |
|||
cryptorand "crypto/rand" |
|||
"crypto/sha256" |
|||
"crypto/rand" |
|||
"encoding/hex" |
|||
"fmt" |
|||
"math/big" |
|||
"sync" |
|||
"time" |
|||
) |
|||
|
|||
// Generator handles the generation of various IDs
|
|||
type Generator struct { |
|||
charsetMu sync.RWMutex |
|||
charset string |
|||
} |
|||
// Generator handles secure ID generation for machines and devices
|
|||
type Generator struct{} |
|||
|
|||
// NewGenerator creates a new ID generator with default settings
|
|||
// NewGenerator creates a new ID generator
|
|||
func NewGenerator() *Generator { |
|||
return &Generator{ |
|||
charset: "0123456789ABCDEFGHJKLMNPQRSTVWXYZ", |
|||
} |
|||
return &Generator{} |
|||
} |
|||
|
|||
// SetCharset allows customizing the character set used for ID generation
|
|||
func (g *Generator) SetCharset(charset string) { |
|||
g.charsetMu.Lock() |
|||
defer g.charsetMu.Unlock() |
|||
g.charset = charset |
|||
} |
|||
// Helper methods
|
|||
// -------------
|
|||
|
|||
// GenerateMachineID generates a new machine ID with the format auth0|user_XX[unique_id]
|
|||
func (g *Generator) GenerateMachineID() (string, error) { |
|||
prefix := "auth0|user_" |
|||
// simulateWork adds a small delay to make progress visible
|
|||
func (g *Generator) simulateWork() { |
|||
time.Sleep(800 * time.Millisecond) |
|||
} |
|||
|
|||
// Generate random sequence number between 0-99
|
|||
seqNum, err := cryptorand.Int(cryptorand.Reader, big.NewInt(100)) |
|||
if err != nil { |
|||
return "", fmt.Errorf("failed to generate sequence number: %w", err) |
|||
// generateRandomHex generates a random hex string of specified length
|
|||
func (g *Generator) generateRandomHex(length int) (string, error) { |
|||
bytes := make([]byte, length) |
|||
if _, err := rand.Read(bytes); err != nil { |
|||
return "", fmt.Errorf("failed to generate random bytes: %w", err) |
|||
} |
|||
sequence := fmt.Sprintf("%02d", seqNum.Int64()) |
|||
return hex.EncodeToString(bytes), nil |
|||
} |
|||
|
|||
uniqueID, err := g.generateUniqueID(23) |
|||
if err != nil { |
|||
return "", fmt.Errorf("failed to generate unique ID: %w", err) |
|||
} |
|||
// Public methods
|
|||
// -------------
|
|||
|
|||
fullID := prefix + sequence + uniqueID |
|||
return hex.EncodeToString([]byte(fullID)), nil |
|||
// GenerateMachineID generates a new 32-byte machine ID
|
|||
func (g *Generator) GenerateMachineID() (string, error) { |
|||
g.simulateWork() |
|||
return g.generateRandomHex(32) |
|||
} |
|||
|
|||
// GenerateMacMachineID generates a new MAC machine ID using SHA-256
|
|||
// GenerateMacMachineID generates a new 64-byte MAC machine ID
|
|||
func (g *Generator) GenerateMacMachineID() (string, error) { |
|||
data := make([]byte, 32) |
|||
if _, err := cryptorand.Read(data); err != nil { |
|||
return "", fmt.Errorf("failed to generate random data: %w", err) |
|||
} |
|||
|
|||
hash := sha256.Sum256(data) |
|||
return hex.EncodeToString(hash[:]), nil |
|||
g.simulateWork() |
|||
return g.generateRandomHex(64) |
|||
} |
|||
|
|||
// GenerateDeviceID generates a new device ID in UUID v4 format
|
|||
// GenerateDeviceID generates a new device ID in UUID format
|
|||
func (g *Generator) GenerateDeviceID() (string, error) { |
|||
uuid := make([]byte, 16) |
|||
if _, err := cryptorand.Read(uuid); err != nil { |
|||
return "", fmt.Errorf("failed to generate UUID: %w", err) |
|||
} |
|||
|
|||
// Set version (4) and variant (2) bits according to RFC 4122
|
|||
uuid[6] = (uuid[6] & 0x0f) | 0x40 |
|||
uuid[8] = (uuid[8] & 0x3f) | 0x80 |
|||
|
|||
return fmt.Sprintf("%x-%x-%x-%x-%x", |
|||
uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:16]), nil |
|||
} |
|||
|
|||
// generateUniqueID generates a random string of specified length using the configured charset
|
|||
func (g *Generator) generateUniqueID(length int) (string, error) { |
|||
g.charsetMu.RLock() |
|||
defer g.charsetMu.RUnlock() |
|||
|
|||
result := make([]byte, length) |
|||
max := big.NewInt(int64(len(g.charset))) |
|||
|
|||
for i := range result { |
|||
randNum, err := cryptorand.Int(cryptorand.Reader, max) |
|||
if err != nil { |
|||
return "", fmt.Errorf("failed to generate random number: %w", err) |
|||
} |
|||
result[i] = g.charset[randNum.Int64()] |
|||
g.simulateWork() |
|||
id, err := g.generateRandomHex(16) |
|||
if err != nil { |
|||
return "", err |
|||
} |
|||
|
|||
return string(result), nil |
|||
return fmt.Sprintf("%s-%s-%s-%s-%s", |
|||
id[0:8], id[8:12], id[12:16], id[16:20], id[20:32]), nil |
|||
} |
@ -1,26 +0,0 @@ |
|||
package idgen |
|||
|
|||
import ( |
|||
"testing" |
|||
|
|||
"github.com/stretchr/testify/assert" |
|||
) |
|||
|
|||
func TestNewGenerator(t *testing.T) { |
|||
gen := NewGenerator() |
|||
assert.NotNil(t, gen, "Generator should not be nil") |
|||
} |
|||
|
|||
func TestGenerateMachineID(t *testing.T) { |
|||
gen := NewGenerator() |
|||
id, err := gen.GenerateMachineID() |
|||
assert.NoError(t, err, "Should not return an error") |
|||
assert.NotEmpty(t, id, "Generated machine ID should not be empty") |
|||
} |
|||
|
|||
func TestGenerateDeviceID(t *testing.T) { |
|||
gen := NewGenerator() |
|||
id, err := gen.GenerateDeviceID() |
|||
assert.NoError(t, err, "Should not return an error") |
|||
assert.NotEmpty(t, id, "Generated device ID should not be empty") |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue